EnjoySharePoint YouTube Channel

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.

Get Office 365 Enterprise E3 Or Business Premium Subscription & Try out all the features

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 [Read here].

Follow below steps to create a WCF service:

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:

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:

public interface IService1
string GetData(int value);

CompositeType GetDataUsingDataContract(CompositeType composite);

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;

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.

Now rebuild the project and then run the project. You should able to see the WCF test client dialog box as shown in the figure below, Also you will able to see the 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 the below steps:
Start Visual Studio 2010. Go to File -> New -> Web Site. From New Web Site dialog box selects ASP.NET Web Site. Give a proper name and click on OK as shown in the figure below.

Now Right click on the solution explorer and click on Add Service Reference… as shown in the figure below:

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:

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

Now it is time to call the 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.

Also, you can check out my previous posts on:
Check out Best Alternative to InfoPath -> Try Now
You May Also like the Following SharePoint Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →