Message Contract:
Messsage Contract is Control over the developer. We can customize the various parts of the message through various parts of message using [MessageHeader] and [MessageBodyMember] and [MessageHeaderArray]..ect attributes.
Advantage:
we can control the Message in any level.
Advantage:
we can control the Message in any level.
Message contract has rules:
The in and out messages should be Message based communication
Message Contract Demo:
[ServiceContract]
public interface IService1
{
[OperationContract]
[FaultContract(typeof(CustomError))]
void InsertEmployee(Employee value); // 1st rule
[OperationContract(Name="SingleEmployeereturn")]
Employee GetEmployes(); // 2nd rule
[OperationContract]
Department GetEmployeeDetails(Employeid eid); // 3rd rule
}
[MessageContract]
public class Employee
{
[MessageHeader]
public string EName;
[MessageBodyMember(Order = 1, Name = "EmpLocation")]
public string ELocation;
[MessageBodyMember(Order = 2, Name = "EmpDept")]
public int EDept;
[MessageBodyMember(Order = 3, Name = "EmpUpload")]
public bool Euploadstatus;
}
[MessageContract]
public class Employeid
{
[MessageHeader(Name = "EmpDept")]
public int EDept;
}
[MessageContract]
public class Department
{
[MessageBodyMember(Order=1,Name="DeptNames")]
public string DeptName;
[MessageBodyMember(Order=0,Name="DeptLocations")]
public string DeptLocation;
}
[DataContract]
public class CustomError
{
[DataMember]
public string ErrorMsg { get; set; }
[DataMember]
public string ErrorCode { get; set; }
}
Service Implplementation
public class Service1 : IService1
{
SqlConnection con = new SqlConnection("uid=sa;pwd=zolt123$;database=wcfdb");
SqlDataAdapter da;
DataSet ds;
public void InsertEmployee(Employee eobj)
{
da = new SqlDataAdapter("insert into emp values('" + eobj.EName + "','" + eobj.ELocation + "'," + eobj.EDept + "," + Convert.ToInt32(eobj.Euploadstatus) + ")", con);
ds = new DataSet();
da.Fill(ds);
}
public Employee GetEmployes()
{
da = new SqlDataAdapter("select * from emp where Eid=" + 101, con);
ds = new DataSet();
da.Fill(ds);
Employee eobj = new Employee();
eobj.EName = ds.Tables[0].Rows[0][1].ToString();
eobj.ELocation= ds.Tables[0].Rows[0][2].ToString();
eobj.EDept= Convert.ToInt32(ds.Tables[0].Rows[0][3].ToString());
eobj.Euploadstatus = Convert.ToBoolean(ds.Tables[0].Rows[0][4].ToString());
return eobj;
}
public Department GetEmployeeDetails(Employeid eid)
{
da = new SqlDataAdapter("select * from dept where did=" + eid.EDept, con);
ds = new DataSet();
da.Fill(ds);
Department dept = new Department();
dept.DeptName = ds.Tables[0].Rows[0][1].ToString();
dept.DeptLocation = ds.Tables[0].Rows[0][2].ToString();
return dept;
}
}
Service is succefully running.
At client side:
using WCFMessageContractClient.ServiceReference1;
static void Main(string[] args)
{
using (Service1Client proxy=new Service1Client())
{
try
{
proxy.InsertEmployee("dani11", "hyd", 1, false);
Console.WriteLine("inserted sussfully........1st rule..");
Employee emp = new Employee();
string loc;
int depts;
bool uploadstatus;
proxy.SingleEmployeereturn(out loc, out depts, out uploadstatus);
Console.WriteLine(loc);
Console.WriteLine(depts);
Console.WriteLine(uploadstatus);
Console.Write("record succefully return to client....2nd ruel..");
string dlocation;
// BASED ON SERIALIZATION OF WCF
string dloc = proxy.GetEmployeeDetails(4, out dlocation);
Console.WriteLine(dlocation);
Console.WriteLine(dloc);
Console.Write("In and out parameters are messagecontract types ....3nd ruel..");
Console.Read();
}
catch(Exception ex)
{
proxy.Abort();
Console.Write("insert failed");
Console.Read();
}
}
}
Message Contract
Message uses xmlserializer for serialization and dserialization.
We can use Datacontract type as parameter inside message contract.
Like
We can use datacontract as parameter to Message Contract
[MessageContract]
public class Countary
{
[MessageBodyMember(Order = 1, Name = "CountryName")]
public string CountryName;
[MessageBodyMember(Order = 2, Name = "CountryLocation")]
public State CountryState;
}
[DataContract]
public class State
{
string statename;
[DataMember]
public string Statename
{
get { return statename; }
set { statename = value; }
}
}
public interface IService1
{
[OperationContract]
[FaultContract(typeof(CustomError))]
Countary GetStates();
}
Public class Service: IService1
{
public Countary GetStates()
{
Countary cobj = new Countary { CountryName = "India",
CountryState = new State { Statename = "Andhra Pradesh" } };
return cobj;
}
}
At Client
using (Service1Client proxy=new Service1Client())
{
try
{
State statename = new State();
string strstates=proxy.GetStates(out statename);
Console.WriteLine(strstates);
Console.WriteLine(statename.Statename);
Console.Read();
}
catch(Exception ex)
{
proxy.Abort();
Console.Write("insert failed");
Console.Read();
}
}
Here State is DataContract and Country is Message Contract. And State is used as Parameter of return type in Messagecontract type.
As well we can’t mix datacontract and message contract in single operation.
But we can mix typed and untyped messages in an operation.
No comments:
Post a Comment