wcf
  1. wcf-contraacts

Contracts - (WCF)

In WCF (Windows Communication Foundation), a contract is a formal agreement between a client and a service that defines the operations that can be performed, the data types that can be exchanged, and how the communication will take place.

Syntax

There are three types of contracts in WCF:

  • Service Contract
  • Data Contract
  • Message Contract

Here is the basic syntax for a service contract in WCF:

using System.ServiceModel;

[ServiceContract]
public interface IMyService
{
  [OperationContract]
  string GetData(int value);
}

And here is the basic syntax for a data contract in WCF:

using System.Runtime.Serialization;

[DataContract]
public class MyData
{
  [DataMember]
  public string Name { get; set; }

  [DataMember]
  public int Age { get; set; }
}

Example

Here is an example of how to define and use a service contract and data contract in WCF:

using System.ServiceModel;

[ServiceContract]
public interface IMyService
{
  [OperationContract]
  MyData GetData(int value);
}

[DataContract]
public class MyData
{
  [DataMember]
  public string Name { get; set; }

  [DataMember]
  public int Age { get; set; }
}

public class MyService : IMyService
{
  public MyData GetData(int value)
  {
    // do some work to retrieve data
    var data = new MyData
    {
      Name = "John Doe",
      Age = 30
    };
    return data;
  }
}

Output

When you call the GetData method on an instance of the MyService class, you should receive a MyData object containing the name and age of a person.

Explanation

In the example code, we define a service contract using the [ServiceContract] attribute on an interface. This interface defines the operations that a client can perform on the service. Inside the interface, we define an operation contract using the [OperationContract] attribute, which is a method that can be invoked by the client.

We also define a data contract using the [DataContract] attribute on a class. This class defines the data that can be exchanged between the client and the service. Inside the class, we define data members using the [DataMember] attribute, which are the individual pieces of data that can be exchanged.

Finally, we implement the service contract in a class that implements the interface we defined earlier. Inside this class, we define the logic for the operation contract, which in this case is a method that returns a MyData object.

Use

Contracts are the foundation of WCF, providing a formal agreement between a client and a service that defines the operations that can be performed and the data types that can be exchanged. Service contracts define the operations that a client can perform, data contracts define the data that can be exchanged, and message contracts provide more control over the way data is transmitted over the wire.

Important Points

  • Contracts are a fundamental concept in WCF, defining the formal agreement between a client and a service.
  • Service contracts define the operations that a client can perform on the service.
  • Data contracts define the data that can be exchanged between the client and the service.
  • Message contracts provide more control over the way data is transmitted over the wire.

Summary

In this page, we discussed contracts in WCF, a formal agreement between a client and a service that defines the operations that can be performed, the data types that can be exchanged, and how the communication will take place. We covered the syntax, example, output, explanation, use, important points, and summary of contracts in WCF. Understanding contracts is crucial for developing WCF services and clients that can communicate effectively and reliably.

Published on: