Search This Blog

2009-12-20

Create Multi Layer Architecture using WCF

WCF is the Microsoft next generation technology for developing distributed applications.WCF is based on SOA.So,if we want to expose our component as a service,we can use WCF to host the service in IIS,WAS,Self Hosting or managed windows service.
In the following example ,the main component is written in CalculateNumber class which contains a method Calculate.The calculate method takes two integer number and return an integer.So this is the component class.
Now we have created CalculateServiceLayer class in the another project to make the component class(CalculateNumber ) to be available in the WCF Host.So in one side ,it will contain System.ServiceModel namespace for WCF and on the other side it will refer the component class CalculateNumber.
Then as we need to host the service we need a console application projectHost) to hosting the service.The Host application configure the necessary settings like address,binding and contract and start the service in a particular address mentioned in the end point.
Now the service is available for invoking.So we will create a client application (web/windows) to invoke the service.
Lets start the step by step development.

Step 1.Create a Class Library Project "CalculateNumber"


Step 2. Write a method in the class and build your component class CalculateNumber.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalculateNumber
{
public class CalculateNumber
{
public int Calculate(int x, int y)
{
return x + y;
}
}
}


Step 3.Add a new project (Service Layer) in the solution


Step 4.Choose a class library project and put the project name "CalculateServiceLayer".


Step 5.Add a reference of System.ServiceModel and your component class dll i.e CalculateNumber.dll


Step 6.Modify the class name Class1.cs to ICalculateSevice.cs and copy the entire code in place of existing code in Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalculateNumber;
using System.ServiceModel;

namespace CalculateServiceLayer
{
[ServiceContract]
interface ICalculateSevice
{
[OperationContract]
int CalcalculateNumber(int m,int n);
}
}

Step 7.Add an another class file CalculateServiceType.cs and write the following code.Then build your application


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalculateNumber;
using System.ServiceModel;

namespace CalculateServiceLayer
{
public class CalculateServiceType:ICalculateSevice
{
CalculateNumber.CalculateNumber cn;
#region ICalculateSevice Members

public int CalcalculateNumber(int m, int n)
{
cn=new CalculateNumber.CalculateNumber();
return cn.Calculate(m, n);
}

#endregion
}
}

Step 8.Now add a Host console application in the same solution


Step 9.Add a reference of System.ServiceModel and CalculateServiceLayer.dll.
Open the Program.cs and write the code as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using CalculateServiceLayer;

namespace Host
{
class Program
{
static void Main(string[] args)
{
ServiceHost sh = new ServiceHost(typeof(CalculateServiceLayer.CalculateServiceType));
sh.Open();
Console.WriteLine("The Service is ready to use");
Console.ReadKey(true);
sh.Close();

}
}
}

Step 10.Add an App.config file in your Host project


Step 11.Write the necessary configuration in your App.config file as follows
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior" name="CalculateServiceLayer.CalculateServiceType">
<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration=""
contract="CalculateServiceLayer.ICalculateSevice" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:3333/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>

Step 12.Now your overall solution structure will be as follows


Step 13.Right click on your Host application and click on "Start new instance" under Debug menu


Step 14.You can see the following console output


Step 15.Now create a web application that will invoke the Wcf service.Right click on the Reference and click on "Add service reference".


Step 16.Put the address "http://localhost:3333/" ,click on Go and then select the service and click OK.Before that make sure your host application is running.


Step 17.Now implement the service in your aspx page as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ImplementCalculateNumber
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ServiceReference1.CalculateSeviceClient myproxy = new ImplementCalculateNumber.ServiceReference1.CalculateSeviceClient();
Response.Write(myproxy.CalcalculateNumber(19, 30).ToString());
}
}
}

Step 18.Run your application ,you can see the following output


Step 19.If you modify in the component or service layer,update the service reference in the client (web) application

No comments: