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 [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:
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:
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.
-: FREE PDF e-Book Download :-
51 JSOM Examples in SharePoint Online
You May Also like the Following SharePoint Online Tutorials: