WCF Tutorial
www.Learn2Expert.net A new ASP.Net MVC 4, SSIS, Interview Q/A tutorial - Visit - www.Learn2Expert.net
Skip Navigation LinksHomeContractsMessage ContractMessage Contract Properties No of Views: 130478
WCF Tutorial | Message Contract Properties

Message Contract Properties

ProtectionLevel

You can mention the MessageHeader or MessageBodyMember to be signed or Encrypted using ProtectionLevel property.

Example
 
using System.Net.Security;

    [MessageContract]
    public class EmployeeDetails
    {
        [MessageHeader(ProtectionLevel=ProtectionLevel.None)]
        public string EmpID;
        [MessageBodyMember(ProtectionLevel = ProtectionLevel.Sign )]
        public string Name;
        [MessageBodyMember(ProtectionLevel = ProtectionLevel.Sign )]
        public string Designation;
        [MessageBodyMember(ProtectionLevel=ProtectionLevel.EncryptAndSign)]
        public int Salary;

    }

In the above type definition, we have made the different protection level for body. But the protection level of the body is determind by the highest ProtectionLevel property. By default if you are not specifying the protection level it takes 'EncryptAndSign'. So it good if you specify minimum ProtectionLevel required.

Name and Namespace:

SOAP representation of the message element can be change by mentioning Name and Namespace property of the Header and Body member. By default namespace is the same as the namespace of the service contract that the message is participating. In the below example, I have mention the Name property to the EmpID and Name.

 
[MessageContract]
    public class EmployeeDetails
    {
        [MessageHeader(Name="ID")]
        public string EmpID;
        [MessageBodyMember(Name="EmployeeName")]
        public string Name;
        [MessageBodyMember()]
        public string Designation;
        [MessageBodyMember()]
        public int Salary;

    }

When SOAP message representation, its name is changed to ID and EmployeeName.

 
<EmployeeDetails>
  <ID>45634</ID>
  <EmployeeName>Sam</EmployeeName>
  <Designation>Software Engineer</Designation>
  <Salary>25000</Salary>
</EmployeeDetails>

Order

The order of the body elements are alpehabetical by default. But you can control the order, usiing Order property in the MessageBody attribute.

 
[MessageContract]
    public class EmployeeDetails
    {
        [MessageHeader()]
        public string EmpID;
        [MessageBodyMember(Order=2)]
        public string Name;
        [MessageBodyMember(Order=3)]
        public string Designation;
        [MessageBodyMember(Order=1)]
        public int Salary;

    }

Tips!

  • 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.