Wednesday, June 3, 2009

A Step by Step WCF Small Programme For Beginners

Abstract

In this article, i am gonna show you a small Addition programme using WCF. After going through this snippet, reader gets a clear and basic understanding of the WCF programme.

Introduction

WCF(Windows Communication Framework) is a unification technology, which unites the following technologies:-• NET remoting• MSMQ• Web services• COM+. It is based on SOA(Service Oriented Architecture). Read basic before go through this article.

Code Snippet
(Service/Server Side)

Steps Involved:-
Step 1: Open VS2008 , create project and choose "Windows Service Application" just give any name to your project. I named it "MyService".

Step 2: You will see the solution window, in that open "IMyService.cs". In that you will see [ServiceContract] below this your interface name is declared. There after you'll see [OperationContract] your function contract should be define here. The implementation of the function will be define in "MyService.svc.cs" As provided in the picture.




Step 3: Open "MyService.svc.cs" and write code here. As i have written code for my "addfunction". You can write your code inside your function. The picture is shown below.

Step 4: This is most important step. In this we are declaring the end point. Inside the we can define end point, as shown in picture end point is defined automatically. we can also define it by programme.


Step 5: Save the project and run it. This will Display like this. Copy the address from explorer address bar.



Code Snippet
(Client Side)
Let the server service run(the above page).

Step 1: Open a web application in another VS2008 and right click on solution name and then go to "Add Service Reference" one window will open paste the previously copied link to the address bar and press GO. Then the service will appear in service section then press OK. The service reference will appear in solution explorer as below.


Step2: Now add one button on the default page, then double click on button you'll be in code behind section. Here create the object of the service which appeared in the solution explorer. Something like this.

protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.MyServiceClient cls = new wcfProxycall.ServiceReference1.MyServiceClient();
cls.Open();
Response.Write( cls.addData(5, 4));
cls.Close();
}



Step 3: Now save the project and run it. If every thing goes fine then the output will be like this.



Hope this article may helped you to understand and build a simple addition programme in WCF.

Thanks.