This post is for beginners who are starting in WCF. I will be focused and help you to create and consume first WCF Service in simplest steps. We will follow steps as following,
At the end of this post you should able to create first WCF Service and consume that in a Console Application. I will keep adding further posts in this series to make WCF easier for you.
- Launch any edition of Visual Studio 2010 or 2012.
- Create a project by choosing WCF Service Application project template from WCF tab.
- Delete default created IService1.cs and Service1.svc file.
Step 1: Create Service Contract
To create a Service Contract,
- Right click on project and add New Item
- From Web tab choose WCF Service to add.
- Let us give Calculator.svc name of service.
Next you need to perform following tasks,
- Open ICalculator.cs and remove void DoWork() function.
- Make sure attribute of Interface is set as [ServiceContract]
- Define functions you want to create as part of Contract
- Set Attribute of functions as [OperationContract]
You will create ICalculator Service Contract with four basic calculator operations as following,
public interface ICalculator
double AddNumbers(double number1, double number2);
double SubstractNumbers(double number1, double number2);
double MultiplyNumbers(double number1, double number2);
double DivisionNumbers(double number1, double number2);
Above you have created a Service Contract with four Operation Contracts. These contracts will be part of Service Contract and exposed to clients. By this step you have created ICalculator Service Contract.
Step 2: Expose Endpoints with Metadata
In this step you need to expose Endpoints and Metadata of service. To do this open Web.config file. We are going to create one Endpoint with basicHttpBinding. We are adding metadata Endpoint also to expose metadata of service. We need metadata at the client side to create proxy.
<endpoint address="" contract="fourstepblogdemo.ICalculator" binding="basicHttpBinding"/>
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
Step 3: Implement Service
In this step we need to implement service. To implement service open Calculator.svc and write codes to implement functions is defined in ServiceContract ICalculator.cs. Delete implementation of DoWork function from Calculator.svc and implement services as following
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Calculator" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Calculator.svc or Calculator.svc.cs at the Solution Explorer and start debugging.
public class Calculator : ICalculator
public double AddNumbers(double number1, double number2)
double result = number1 + number2;
public double SubstractNumbers(double number1, double number2)
double result = number1 - number2;
public double MultiplyNumbers(double number1, double number2)
double result = number1 * number2;
public double DivisionNumbers(double number1, double number2)
double result = number1 / number2;
As of now you have created Service and configured Endpoint. Now you need to host service. There are many processes in which a WCF Service can be hosted. Some processes are
- Managed Application
- ASP.Net Web Server
- Windows Service
- App Fabric
In this post we are not going into details of WCF Service hosting and we will consider simplest hosting option. Let us host service in ASP.Net Web Server. To host press F5 in visual studio.
In browser you can see Service as following.
To view metadata of Service click on URL of wsdl. You may notice that Service is hosted on localhost.
Step 4: Consume Service
There are various ways a WCF SOAP Service can be consumed in different kind of clients. In this post we will consume service in a Console Application. Launch Visual Studio and create Console Application project.
Now there are two ways you can create proxy at client side.
- Using svcuitl.exe at command prompt
- By adding Service Reference
In this post we will create proxy at client side using Add Service Reference. In Console Project right click on Reference and select option of Add Service Reference. In Add Service Reference dialog copy paste address of Service or if WCF Service and Console Client project is in same solution then click on Discover. If there is no error in Service then you will find Service Reference added as given in following image. If you want you can change name of Reference. I am leaving here default name ServiceReferenc1.
You can consume service at client as following,
- Create instance of proxy class
- Call different operations from service
You can create instance of proxy class as following
And let us say you want to make a call to Add function. That can be done as following
So at client side you can call all four functions of Calculator Service as following
static void Main(string args)
CalculatorClient proxy = new CalculatorClient();
double addResult = proxy.AddNumbers(9, 3);
Console.WriteLine("Result of Add Operation");
double subResult = proxy.SubstractNumbers(9, 3);
Console.WriteLine("Result of Substract Operation");
double mulResult = proxy.MultiplyNumbers(9, 3);
Console.WriteLine("Result of Multiply Operation");
double divResult = proxy.MultiplyNumbers(9, 3);
Console.WriteLine("Result of Division Operation");
Press F5 to run Console Client Application. You will get desired result.
Just now you have create a Calculator WCF Service and consumed that in a Console Application. In further posts I will simplify other concepts for WCF for beginners. I hope you find this post useful. Thanks for reading.