Powered by Blogger.

Sunday, March 2, 2014

WCF tutorial and example in C#.Net




In this post we will see how to create a WCF service and how we can consume the WCF service.

Before reading this you can check Tutorial on WCF and Advantages of WCF. You can also check a good article on Host a WCF service in Windows service in C#.net.

Follow below steps to create a WCF service:

Step-1:
Open Visual Studio 2010, then go to File -> New -> Project. Then from the New Project dialog box, From the Installed Templates Select WCF from the left hand site, And then Choose WCF Service Library and Give a proper name and Click on OK as shown in the figure below:


Step-2:
Now if you will look at the solution explorer, then 3 files are important App.Config, IService1.cs and Service1.cs.

First Open the IService1.cs file and add a method declaration in that like below:

The full IService1 interface will look like below:

 [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        int Add(int firstNumber, int secondNumber);
        // TODO: Add your service operations here
    }
Now we have declare the Add method in the Interface file, now it is time to define the method in the Service1.cs file. Open the Service1.cs file and define the method like below:

 int Add(int firstNumber, int secondNumber)
        {
            return firstNumber + secondNumber;
        }

Step-3:
As of now do not modify anything in the app.config file. Check the base address http://localhost:8732/Design_Time_Addresses/WcfTestServiceLibrary/Service1/
We will use this address while calling this wcf service.

Step-4:
Now rebuild the project and then run the project. You should able to see the WCF test client dialogbox as shown in the figure below, Also you will able to see our custom method that we have added.



Do not stop the service. This finishes our WCF creation part. Now it is time to consume the WCF service.

To consume the WCF service follow below steps:
Step-1:
Start Visual Studio 2010. Go to File -> New -> Web Site. From New Web Site dialog box select ASP.NET Web Site. Give a proper name and click on OK as shown in the figure below.



Step-2:
Now Right click on the solution explorer and click on Add Service Reference... as shown in the figure below:


Step-3:
Now in the Add service Reference dialog box give the base WCF URL (You can get from the App.Config file) and click on GO. If the service is running then it will find the service, Here you can give a Namespace and then click on OK as shown in the figure below:


Step-4:
After you add the Service reference, it will create a folder with name as App_WebReferences and put the necessary files there.

Step-5:
Now it is time to call our Add method that we have defined in the WCF service.

Open the Default.aspx.cs file and write the using statement to add the wcf reference namespace like below:
using ServiceReference1;

Now it is time to call the method. For this first create an object of the Service1Client class and the call the method. The full code is given below:

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

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Service1Client svcClient = new Service1Client();
        int result = svcClient.Add(5, 5);
    }
}

Here result will contain value of 10.



0 comments

Post a Comment