Message is a packet of data which contains important
information. WCF use this message to transfer from source to destination path.
WCF uses a SOAP message for communication. SOAP message
contain message Envelop, Header and body.
Message contract is used to control the structure of a
message body and serialization process. It is used to send/access the
information over SOAP. Using message contract you can customize the parameter
sent using a SOAP message between the client and the server.
How to Define/Declare MessageHeader and
MessageBody?MessageHeader:-
A Message Header is applied to a member of a message contract to declare the member within the message header.
[MessageContract(IsWrapped
= true)]
public class EmpRequest{
[MessageHeader(Name = "EmpIdentity")]
public string EmpId;
}
Message Body:-
A
Message Body Member is applied to a member of a message contract to declare the
member within the message body.
[MessageContract(IsWrapped
= true)]
public class EmpResponse
{
[MessageBodyMember]
public Employee
EmpINfo;
}
Example:-
Create WCF Service application.
Code for IService.cs :-
using System;
using
System.Collections.Generic;
using System.Linq;
using
System.Runtime.Serialization;
using System.ServiceModel;
using
System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the
"Rename" command on the "Refactor" menu to change the
interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
[FaultContract(typeof(string))]
EmpResponse GetEmpInfo(EmpRequest request);
// TODO: Add your service operations here
}
// Use a data contract as
illustrated in the sample below to add composite types to service operations.
[MessageContract(IsWrapped
= true)]
public class EmpRequest
{
[MessageHeader(Name = "EmpIdentity")]
public string
EmpId;
}
[MessageContract(IsWrapped
= true)]
public class EmpResponse
{
[MessageBodyMember]
public Employee
EmpINfo;
}
[DataContract]
public class Employee
{
[DataMember(Name = "FirstName", Order = 1)]
public string
FirstName;
public string
firstName
{
get { return
firstName; }
set { firstName = value; }
}
[DataMember(Name = "LastName", Order = 2)]
public string
LastName;
public string
lastName
{
get { return
lastName; }
set { lastName = value; }
}
[DataMember(Name = "Address", Order = 3)]
public string
Address;
public string
address
{
get { return
address; }
set { address = value;
}
}
}
Code for Service.cs :-
using System;
using
System.Collections.Generic;
using System.Linq;
using
System.Runtime.Serialization;
using System.ServiceModel;
using
System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the
"Rename" command on the "Refactor" menu to change the class
name "Service" in code, svc and config file together.
public class Service : IService
{
public string
GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType
GetDataUsingDataContract(CompositeType
composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue
+= "Suffix";
}
return composite;
}
private const
string EmpId = "d2nf@5";
public EmpResponse
GetEmpInfo(EmpRequest request)
{
EmpResponse response = new EmpResponse();
if (request.EmpId != EmpId)
{
string
error = "Invalid Emp Id";
throw
new FaultException<string>(error);
}
response.EmpINfo.firstName = "Sushil";
response.EmpINfo.lastName = "Modi";
response.EmpINfo.address = "Bangalore"
return response;
}
}
Now deploy the Service on IIS or Local Server.
Create one Windows or Console application.
Add service reference into your application.
After adding service reference, create an object
of Service Client proxy class and call the GetEmpInfo method and display the
Employee info as given below.
class Program
{
static void Main(string[] args)
{
Try
{
ServiceReference2.ServiceClient ref2 = new
ServiceClient();
ServiceReference2.Author a = ref2.GetAuthorInfo("d2nf@5");
Console.WriteLine("First
Name: " + a.firstName + "\t"
+ "Last Name" + a.lastName + "\t" + "Address
: " + a.address);
}
catch (FaultException<string> ex)
{
Console.Write(ex.Detail + "<br/>");
}
}
If you pass wrong EmpId the it show
“Invalid Emp Id”.