WCF Tutorial | Message Contract
Message is the packet of data which contains important information. WCF uses these messages to transfer information from Source to destination.
WCF uses SOAP(Simple Object Access Protocol) Message format for communication. SOAP message contain Envelope, Header and Body.SOAP envelope contails name, namespace,header and body element. SOAP Hear contain important information which are not directly related to message. SOAP body contains information which is used by the target.
Diagram Soap envelope
It describes how the programs will exchange message each other. There are three way of communication between source and destination
- Simplex - It is one way communication. Source will send message to target, but target will not respond to the message.
- Request/Replay - It is two way communications, when source send message to the target, it will resend response message to the source. But at a time only one can send a message
- Duplex - It is two way communication, both source and target can send and receive message simultaniouly.
What is Message contract?
As I said earlier, WCF uses SOAP message for communication. Most of the time developer will concentrate more on developing the DataContract, Serializing the data, etc. WCF will automatically take care of message. On Some critical issue, developer will also require control over the SOAP message format. In that case WCF provides Message Contract to customize the message as per requirement.
WCF supports either RPC(Remote Procedure Call) or Message style operation model. In the RPC model, you can develop operation with Ref and out parameter. WCF will automatically create the message for operation at run time. In Message style operation WCF allows to customize the message header and define the security for header and body of the message.
Defining Message Contract
Message contract can be applied to type using MessageContract attribute. Custom Header and Body can be included to message using 'MessageHeader' and 'MessageBodyMember'atttribute. Let us see the sample message contract definition.
public class EmployeeDetails
public string EmpID;
public string Name;
public string Designation;
public int Salary;
public string Location;
When I use this EmployeeDeatils type in the service operation as parameter. WCF will add extra header call 'EmpID' to the SOAP envelope. It also add Name, Designation, Salary, Location as extra member to the SOAP Body.
You have to follow certain rules while working with Message contract
- When using Message contract type as parameter, Only one parameter can be used in servicie Operation
void SaveEmployeeDetails(EmployeeDetails emp);
- Service operation either should return Messagecontract type or it should not return any value
- Service operation will accept and return only message contract type. Other data types are not allowed.
EmployeeDetails ModifyEmployeeDetails(EmployeeDetails emp);
If a type has both Message and Data contract, service operation will accept only message contract.
- Always create the service with Interface->Implementation format, mention the contract in Interface.
- Define the service in Class library and refer the class library in Host project. Don’t use service class in host project.
- Change the instance mode to per call as default.
- Always catch exception using try/catch block and throw exception using FaultException < T >.
- Logging and Include exception should be enable while compiling the project in debug mode. While in production deployment disable the logging and Include exception details.