Monday 28 December 2015

Difference between DataContract and MessageContract in WCF

Data Contract in WCF is an agreement between a service and a client that describes what type of data will be exchanged between them? I other words, Message Contract describes the structure of SOAP message that is passed between  service and client. Using Data Contract, we actually control the contents ( message body) of a SOAP message while Message Contract provides complete control over structure of SOAP message.

In Windows Communication Foundation, there are two types of Contracts:

  1. Behavioral Contracts
  2. Structural Contracts

Both DataContract and MessageContract are structural contracts that compliment each other serving different purposes. Lets now understand the difference between DataContract and MessageContract with the help of an example.



DataContract Example:
Consider a WCF Service having an operation as follows:


[ServiceContract()]
public interface IStudentService
{
    [OperationContract()]
    StudentInfo GetStudentById(int studentId);
}

Here the return type is StudentInfo which is basically a CLR type defined below as a DataContract.


[DataContract(Namespace="http://www.something.com/students/")]
public class StudentInfo
{
       [DataMember(Name="StudentId", Order=1)]
       public int ID;

       [DataMember(Name="FirstName", Order=2)]
       public string FName;

       [DataMember(Name="LastName", Order=3)]
       public string LName;
}



When the service method will be called, this StudentInfo type will be returned as part of the SOAP response message body.
If we look into this "StudentInfo" Data Contract, we are providing different details about data being transferred back to consumer. For example,
Order = 1,2,3 defines the order of the data member in response message body.
Name = "some name" dictates the name of particular data member as part of response message body.
But through all these attributes, we are actually controlling the contents of the SOAP message body only. And in most of the cases, developers are concerned with controlling the body part of the message instead of whole SOAP message.

MessageContract Example:

As we have seen in above example that DataContract has limited control over the SOAP message and all that control is related to contents inside body of the SOAP message. But there are scenarios, when we need more control over SOAP message. So, MessageContract is the answer in such cases.

Now consider a business scenario, an authentication code is required to access our service i.e. IStudentService. In this case functionality provided by our service will remain the same but authentication code validity is additional pre-requisite now. So in such cases, SOAP message header is the most reasonable place to store the authentication code while calling the service method.

Look into the below implementation of this scenario:

[MessageContract(IsWrapped = false)]
public class StudentInfoRequestMessage
{
       [MessageHeader()]
       public string AuthCode;

      [MessageBodyMember()]
      public int StudentId;
}

[MessageContract(IsWrapped = false)]
public class StudentInfoResponseMessage
{
      [MessageBodyMember()]
      public StudentInfo MyStudentInfo;
}

[ServiceContract]
public interface IStudentService
{
        [OperationContract]
        StudentInfoResponseMessage GetStudentById(StudentInfoRequestMessage stdInfoMessage);
}

public class StudentService : IStudentService
{
  public StudentInfoResponseMessage GetStudentById(StudentInfoRequestMessage stdInfoMessage)
     {
         private const string AuthCode = "ali123salman";

               //Validation Check
               if (stdInfoMessage.AuthCode != AuthCode)
               {
                       //fault response
               }    
              //routine code
     }
}



In this above post I explained Difference DataContract and MessageContract in WCF and how to choose one of them. I hope its helpful to reader so please comments your feedback and queries.

What are session modes in WCF? How can you make a service as sessionful?



ServiceContract attribute offers the property SessionMode which is used to specify the session mode. There are three session modes supported by WCF:


  • Session.Allowed(default): Transport sessions are allowed, but not enforced. Service will behave as a per-session service only if the binding used maintains a transport-level session.
  • Session.Required: Mandates the use of a transport-level session, but not necessarily an application-level session.
  • Session.NotAllowed: Disallows the use of a transport-level session, which precludes an application-level session. Regardless of the service configuration, the service will always behave as a per-call service.




[ServiceContract(SessionMode = SessionMode.NotAllowed)]
interface IMyContract
{.................}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
class MyService : IMyContract
{.................}








In this above post I explained you can limit how many instances or sessions are created at the application level