Few years back, when WCF was introduced, i was very eager to learn about this technology. I got few chances to work on it and i came across the concept of FaultContract and MessageContract. But was not able to dig deep into these concepts. Finally, I decided to spend some time to these concepts of MessageContracts as well.
What is Message Contract
While using wcf service, in order to send the data, like any custom class data, developers don’t have to do anything except adding the DataContract & DataMember attributes on the type of data, which is to be transferred. Developer need not to be concerned about serializing the data, converting it into SOAP xml and sending to the client. To check this, let’s create a sample application.
Add a new WCF application and add 2 classes named RequestData and ResponseData. RequestData is having a property named EmployeeId and ResponseData is having properties representing the details about the Id being sent by the client.In order to serialize this data and transfer it using SOAP, we will only add DataContract and DataMember attributes to these classes and there properties respectively.
Next, we will add a method in our service interface, which will take an input of type RequestData and return output of type ResponseData. Depending on the EmployeeId received in the request, it will return data of the user. So our implementation will be like the following :
Now, run the wcf test client, set a sample EmployeeId and invoke the method to see the results. We can see that the request and response SOAP xml’s contain the EmployeeId that we had set and its corresponding details respectively.
This is the normal way of sending the data through the SOAP. Sometimes we may have situation, where we need to control the way data is being sent, like we may need to send the data in the header of the SOAP or control the overall structure of what data is being sent in the header or body etc. In that case, using DataContract & DataMember is not the solution.
So let’s change some of our existing requirement and approach, where, we would like to send the EmployeeId in the header of the request xml and get back the EmployeeName, EmployeeDesignation in body of the response xml and EmployeeUserName in the header of response xml. To do this, we need to alter our RequestData and ResponseData classes slightly and decorate them with MessageContract and its related attributes named MessageHeader and MessageBodyMember attributes. For the data which we want to send in the header, we add MessageHeader attribute and for data that we want to send in body xml, we add MessageBodyMember attribute. On the whole, we add the MessageContract attribute on these classes, so that these can be serialized for transfer. So our changed code becomes :
So all set now. We just need to run the wcf test client and invoke the method again and we can see the results.
This what we changed the code for. We have the EmployeeId in request header xml and EmployeeUserName in the response header xml. Other details are added in the response body xml. But using this process has got certain restrictions on it. These are :
- Any method involving MessageContract types, can have only one input parameter and that too has to be a MessageContract type.
- The return value can be either be void or any type that is of MessageContract type.
So this is the basic implementation of MessageContract. If you need to control the overall structure of the SOAP xml being used, you need to use MessageContract. Hope you enjoyed reading it…!!!