Today, in this article let's see the concept of Message Contract in WCF and we
will try to implement in this session.
Questions Arises: What is Message Contract?
WCF messages that are used to process from one point to other point which
consists of data packet information with header and body. It uses Soap Format
processing. It provides customized way to send the information to requested
client.
Question Arises: What are MessageHeader and MessageBodyMember ?
- Message Header: Describes the header of
envelop to send message across.
- Message Body Member: Describe the data or
body of envelop where all things are integrated into message to send across
requested clients.
Question Arises: How Message Contract
Differs from Data Contract?
Message Contract is that which describes how the SOAP messages are sent to and
from services with header and bodies in the envelop. If you are more prescribed
about sending message and where by concentrating on layout then I would suggest
you go for message contract. On the other hand, the Data Contracts describes the
data structure with supported data types how the client can consume the WCF
Service. In other words, It is a standardize relationship between service and
client to exchange data.
So, now we all are good to go and implement this concept practically.
The Complete Code of MessageClass.cs looks like this:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using System.Web;
using
System.ServiceModel;
namespace
MessageContractWCF
{
[MessageContract]
public class
MessageClass
{
private string
_typeofOperation;
private double
_firstNumber;
private double
_secondNumber;
private double
_output;
public MessageClass()
{
}
public MessageClass(string
operation, double firstnum,
double secondnum,
double outputresult)
{
this._typeofOperation = operation;
this._firstNumber = firstnum;
this._secondNumber = secondnum;
this._output = outputresult;
}
public MessageClass(MessageClass
simple)
{
this._typeofOperation =
simple._typeofOperation;
this._firstNumber =
simple._firstNumber;
this._secondNumber =
simple._secondNumber;
this._output = simple._output;
}
[MessageHeader]
public string
TypeofOperation
{
get {
return _typeofOperation; }
set { _typeofOperation =
value; }
}
[MessageBodyMember]
public double
FirstNumber
{
get {
return _firstNumber; }
set { _firstNumber =
value; }
}
[MessageBodyMember]
public double
SecondNumber
{
get {
return _secondNumber; }
set { _secondNumber =
value; }
}
[MessageBodyMember]
public double
Output
{
get {
return _output; }
set { _output =
value; }
}
}
}
The Complete Code of IService1.cs looks like this:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
namespace
MessageContractWCF
{
// NOTE: You can use the "Rename" command on the "Refactor"
menu to change the interface name "IService1" in both code and config file
together.
[ServiceContract]
public interface
IService1
{
[OperationContract] MessageClass Arthimetic(MessageClass data);
}
}
The Complete Code of Service1.svc looks like this:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
namespace
MessageContractWCF
{
// NOTE: You can use the "Rename" command on the "Refactor"
menu to change the class name "Service1" in code, svc and config file together.
public class
Service1 : IService1
{
public MessageClass
Arthimetic(MessageClass data)
{
var result =
new MessageClass(data);
switch (result.TypeofOperation)
{
case
"+": result.Output = data.FirstNumber + data.SecondNumber;
break;
case "-":
result.Output = data.FirstNumber - data.SecondNumber;
break; case
"*": result.Output = data.FirstNumber *
data.SecondNumber;
break;
case "/":
result.Output = data.FirstNumber / data.SecondNumber;
break;
default: result.Output = 0;
break;
}
return result;
}
}
}
The Complete Code of Web.Config looks like this:
<?xml
version="1.0"?>
<configuration>
<system.web>
<compilation
debug="true"
targetFramework="4.0"
/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!--
To avoid disclosing metadata information, set the value below to false and
remove the metadata endpoint above before deployment
-->
<serviceMetadata
httpGetEnabled="true"/>
<!--
To receive exception details in faults for debugging purposes, set the value
below to true. Set to false before deployment to avoid disclosing exception
information
-->
<serviceDebug
includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment
multipleSiteBindingsEnabled="true"
/>
</system.serviceModel>
<system.webServer>
<modules
runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
The Complete Code of WebForm1.aspx looks like this:
<%@
Page Language="C#"
AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs"
Inherits="WCFMessage.WebForm1"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head
id="Head1" runat="server">
<title></title>
<style
type="text/css">
.lbl
{
font-family:
Cambria;
}
</style>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<center>
<table>
<tr>
<td>
<asp:Label
ID="Label1"
runat="server"
CssClass="lbl"
Text="Please Enter First
Number"
Font-Bold="true"
ForeColor="Brown"
Font-Italic="true"></asp:Label>
</td>
<td>
<asp:TextBox
ID="TextBox1"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label
ID="Label2"
runat="server"
CssClass="lbl"
Text="Please Enter
Second Number"
Font-Bold="true"
ForeColor="Brown"
Font-Italic="true"></asp:Label>
</td>
<td>
<asp:TextBox
ID="TextBox2"
runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button
ID="Button1"
runat="server"
Text="Addition"
Width="120px"
OnClick="Button1Click"
BackColor="HotTrack"
/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button
ID="Button2"
runat="server"
Text="Substraction"
Width="120px"
OnClick="Button2Click"
BackColor="HotTrack"
/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button
ID="Button3"
runat="server"
Text="Multiplication"
Width="120px"
OnClick="Button3Click"
BackColor="HotTrack"
/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button
ID="Button4"
runat="server"
Text="Division"
Width="120px"
OnClick="Button4Click"
BackColor="HotTrack"
/>
</td>
</tr>
</table>
<table>
<tr>
<td>
<asp:Label
ID="Label3"
runat="server"
Font-Size="Larger"
CssClass="lbl"
ForeColor="Brown"
Visible="false"></asp:Label>
</td>
</tr>
</table>
</center>
</div>
</form>
</body>
</html>
The Complete Code of WebForm1.aspx.cs looks like this:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
WCFMessage.ServiceReference1;
namespace
WCFMessage
{
public partial
class WebForm1
: System.Web.UI.Page
{
protected void
PageLoad(object sender,
EventArgs e)
{
}
protected void
Button1Click(object sender,
EventArgs e)
{
if (TextBox1.Text ==
"" && TextBox2.Text ==
"")
{
Label3.Text = "<center><b><i>Please
Enter Some Values";
Label3.Visible = true;
}
else
{
var objClient =
new Service1Client();
var msgClass =
new MessageClass();
msgClass.FirstNumber = Convert.ToDouble(TextBox1.Text);
msgClass.SecondNumber = Convert.ToDouble(TextBox2.Text);
msgClass.TypeofOperation = "+";
var msgres =
((IService1)objClient).Arthimetic(msgClass);
Label3.Text = ("  The Addition
of:  " + msgClass.FirstNumber + " 
and  " + msgClass.SecondNumber + " 
is:<center><b><i>  " + msgres.Output);
Label3.Visible = true;
}
}
protected void
Button2Click(object sender,
EventArgs e)
{
if (TextBox1.Text ==
"" && TextBox2.Text ==
"")
{
Label3.Text = "<center><b><i>Please
Enter Some Values";
Label3.Visible = true;
}
else
{
var objClient =
new Service1Client();
var msgClass =
new MessageClass();
msgClass.FirstNumber = Convert.ToDouble(TextBox1.Text);
msgClass.SecondNumber = Convert.ToDouble(TextBox2.Text);
msgClass.TypeofOperation = "-";
var msgres =
((IService1)objClient).Arthimetic(msgClass);
Label3.Text = ("  The
Substraction of:  " + msgClass.FirstNumber +
"  and  " + msgClass.SecondNumber +
"  is:<center><b><i>  " +
msgres.Output);
Label3.Visible = true;
}
}
protected void
Button3Click(object sender,
EventArgs e)
{
if (TextBox1.Text ==
"" && TextBox2.Text ==
"")
{
Label3.Text = "<center><b><i>Please
Enter Some Values";
Label3.Visible = true;
}
else
{
var objClient =
new Service1Client();
var msgClass =
new MessageClass();
msgClass.FirstNumber = Convert.ToDouble(TextBox1.Text);
msgClass.SecondNumber = Convert.ToDouble(TextBox2.Text);
msgClass.TypeofOperation = "*";
var msgres =
((IService1)objClient).Arthimetic(msgClass);
Label3.Text = ("  The
Multiplication of:  " + msgClass.FirstNumber +
"  and  " + msgClass.SecondNumber +
"  is:<center><b><i>  " +
msgres.Output);
Label3.Visible = true;
}
}
protected void
Button4Click(object sender,
EventArgs e)
{
if (TextBox1.Text ==
"" && TextBox2.Text ==
"")
{
Label3.Text = "<center><b><i>Please
Enter Some Values";
Label3.Visible = true;
}
else
{
var objClient =
new Service1Client();
var msgClass =
new MessageClass();
msgClass.FirstNumber = Convert.ToDouble(TextBox1.Text);
msgClass.SecondNumber = Convert.ToDouble(TextBox2.Text);
msgClass.TypeofOperation = "/";
var msgres =
((IService1)objClient).Arthimetic(msgClass);
Label3.Text = ("  The Division
of:  " + msgClass.FirstNumber + " 
and  " + msgClass.SecondNumber + " 
is:<center><b><i>  " + msgres.Output);
Label3.Visible = true;
}
}
}
}
The Output of the Application looks like this:
The Addition Operation Output of the Application looks like this:
The Nothing Entered Values output looks like this:
I hope this article is useful for you.