What Are Contracts in Wcf Service

Contracts in WCF Service: Explained

Windows Communication Foundation (WCF) services are a set of technologies that enable developers to create and deploy distributed applications. WCF services use a contract-based programming model, which means that services are defined by contracts that specify the communication protocol, message exchange pattern, and message format used by the service.

So, what are contracts in WCF service? In simple terms, a contract is an agreement between two parties about how they will communicate with each other. In the context of WCF services, a contract defines the interface that a service exposes to clients and the protocols used to communicate with the service.

There are three types of contracts in WCF services:

1. Service Contract: A service contract defines the interface of the service – the methods, parameters, and return values that the service exposes to clients. The service contract is defined using the ServiceContract attribute in the service code.

2. Data Contract: A data contract defines the data types used by the service – the classes or structures that the service uses to pass data to clients. A data contract is defined using the DataContract attribute in the service code.

3. Message Contract: A message contract defines the format of the messages exchanged between the service and clients. A message contract is defined using the MessageContract attribute in the service code.

Let`s take a closer look at each of these contracts.

Service Contract

The service contract defines the operations that a service provides to clients. These operations are represented as methods on the service interface. The client calls these methods to interact with the service. The service contract also defines the protocols used to communicate with the service – for example, TCP, HTTP, or HTTPS.

Here`s an example of a service contract:

[ServiceContract]

public interface IMyService

{

[OperationContract]

string GetMessage(string name);

}

In this example, the service contract defines a single operation called GetMessage, which takes a string parameter and returns a string value.

Data Contract

The data contract defines the data types used by the service. These data types could be simple types like strings and integers, or complex types like custom classes or structures.

Here`s an example of a data contract:

[DataContract]

public class Person

{

[DataMember]

public string FirstName { get; set; }

[DataMember]

public string LastName { get; set; }

[DataMember]

public int Age { get; set; }

}

In this example, the data contract defines a custom class called Person, which has three properties – FirstName, LastName, and Age. The DataMember attribute is used to mark each property as part of the data contract.

Message Contract

The message contract defines the format of the messages exchanged between the service and clients. This includes the message headers, message body, and any custom message properties.

Here`s an example of a message contract:

[MessageContract]

public class MyMessage

{

[MessageHeader]

public string HeaderValue { get; set; }

[MessageBodyMember]

public string BodyValue { get; set; }

}

In this example, the message contract defines a custom message called MyMessage, which has a message header called HeaderValue and a message body called BodyValue. The MessageHeader and MessageBodyMember attributes are used to mark each property as part of the message contract.

In conclusion, contracts in WCF services are essential for defining the interface, data types, and message formats used by the service. By using contracts, developers can create flexible and interoperable services that can communicate with a wide range of clients.