WCF Contracts, Service Contracts, Data Contracts, Fault Contracts and Message Contracts in WCF

In WCF, all its functionality is exposed by contracts. The contract defines about services and it describes what the service does.

 

WCF contains four different types of contracts. Those are Service Contracts, Data Contracts, Fault Contracts and Message Contracts.

 

Service Contracts:

Services contracts describe, what are the operations client can perform on a particular WCF service.

 

Data Contracts:

Data contracts define, what are different data types WCF service can accept and what are the data types service can provide to the client. By default WCF defines implicit data contracts for built-in data type’s int and string. Even you can create explicit data contracts for custom types.

 

Fault Contracts:

Fault contracts provide what are the error messages provided by the service, how the service handles those errors and it displays those errors to client.

 

Message Contracts:

Message contracts allow the service to interact directly with the message. These contracts can be typed or untyped.

 

Here we discuss about Service Contracts in WCF in-detail.

 

Generally WCF service exposes its functionality through interfaces or classes. By applying ServiceContract attribute on interface, CLR exposes the interface as a WCF contract independently of its scope. If you apply the ServiceContract attribute on a internal interface, CLR exposes that interface as a public service contract ready to be consumed across the assembly boundaries. If you are not applying the service contract, that interface is not visible and WCF client cannot consume it. This theory is applicable to classes also, because you can expose class also to the client by applying the ServiceContract attribute.

 

In the below you will simple example for interface by applying the ServiceContract attribute.

 

namespace WcfServiceApp

{

    // NOTE: If you change the interface name "IServiceContractExp" here, you must also update the reference to "IService1" in Web.config.

    [ServiceContract]

    public interface IServiceContractExp

    {

        //method with operation contract

        [OperationContract]

        string MyFirstMethod(string str);

 

         //method without operation contract

        string MySecondMethod(string str);

    }

}

 

In the above example you applied the ServiceContract attribute to the interface IServiceContractExp, then it exposes to the client as WCF contract. Without ServiceContract attribute, the WCF functionality will not expose to the client.

 

In the above example, in addition with ServiceContract attribute, you find one more attribute OperationContract. OperationContract is used to exposes the methods to the WCF client. Without OperationContract you cannot make visible of your methods to your client, even though you applied to the ServiceContract. In interface IserviceContractExp you have two methods MyFirstMethod, MySecondMethod. MyFirstMethod has the OperationContract attribute, so it is exposed to client and MySecondMethod will not expose to client even it is public because it does not have OperationContract attribute. You can apply OperationContract attribute only on methods, not on properties, indexers or events. OperationContract attribute exposes contract method as a logical function. If any method does not have OperationContract, it does not part of WCF contract and does not visible to client.

 

As I said, you can apply ServiceContract attribute on classes or interfaces. If you apply ServiceContract on interface, some class needs to implement that interface as shown below.

 

namespace WcfServiceApp

{

    // NOTE: If you change the class name "wcfExp" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.

    public class wcfExp : IServiceContractExp

    {

        #region IServiceContractExp Members

        public string MyFirstMethod(string str)

        {

            return "Method with Operation contract";

        }

 

        public string MySecondMethod(string str)

        {

            return "Method without Operation contract and it will not display for client";

        }

        #endregion

    }

}

 

In WCF, single class can implement multiple contracts by implementing the multiple interfaces.

 

To implement any interface which has ServiceContract attribute, class should avoid the parameterized constructor because WCF only use the default constructor and class can use internal properties, static members but those are not in the part of WCF contract.

 

You can assign the namespace to ServiceContract to avoid chances of collision as shown below.

 

[ServiceContract(Namespace = "ServicContractNameSpace")]

public interface IServiceContractExp

{

     //method with operation contract

     [OperationContract]

     string MyFirstMethod(string str);

 

      //method without operation contract

     string MySecondMethod(string str); 

}  

 

Even you can change the name of the ServiceContract to display to the client as shown below.

 

[ServiceContract(Name = "IServiceContract")]

public interface IServiceContractExp

{

     //method with operation contract

     [OperationContract]

     string MyFirstMethod(string str);

 

     //method without operation contract

     string MySecondMethod(string str); 

}  

 

In the above, actually ServiceContract name is IServiceContractExp, but you change it to IServiceContract. So client will identify this service by using IServiceContract contract name.

 

In the same way you can change the name to OperationContract as shown below.

 

[ServiceContract]

public interface IServiceContractExp

{

    //method with operation contract

    [OperationContract(Name = "OperationContractMethod")]

    string MyFirstMethod(string str); 

}

                                                                                          

                                                                                                             WcfServiceApp.zip (18.13 kb)