Four Steps to create first WCF Service: Beginners Series

image

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,

image

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.

Project setup

  1. Launch any edition of Visual Studio 2010 or 2012.
  2. Create a project by choosing WCF Service Application project template from WCF tab.
  3. Delete default created IService1.cs and Service1.svc file.

Step 1: Create Service Contract

To create a Service Contract,

  1. Right click on project and add New Item
  2. From Web tab choose WCF Service to add.
  3. Let us give Calculator.svc name of service.

image

Next you need to perform following tasks,

  1. Open ICalculator.cs and remove void DoWork() function.
  2. Make sure attribute of Interface is set as [ServiceContract]
  3. Define functions you want to create as part of Contract
  4. Set Attribute of functions as [OperationContract]

You will create ICalculator Service Contract with four basic calculator operations as following,

ICalculator.cs


using System.ServiceModel;

namespace fourstepblogdemo
{

 [ServiceContract]
 public interface ICalculator
 {
 [OperationContract]
 double AddNumbers(double number1, double number2);
 [OperationContract]
 double SubstractNumbers(double number1, double number2);
 [OperationContract]
 double MultiplyNumbers(double number1, double number2);
 [OperationContract]
 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.


<services>
 <service name="fourstepblogdemo.Calculator">
 <endpoint address="" contract="fourstepblogdemo.ICalculator" binding="basicHttpBinding"/>
 <endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
 </service>
 </services>


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

Calculator.svc.cs

namespace fourstepblogdemo
{
 // 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;
 return result;
 }

public double SubstractNumbers(double number1, double number2)
 {
 double result = number1 - number2;
 return result;
 }

public double MultiplyNumbers(double number1, double number2)
 {
 double result = number1 * number2;
 return result;
 }

public double DivisionNumbers(double number1, double number2)
 {
 double result = number1 / number2;
 return result;
 }
 }
}


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
  • IIS
  • 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.

image

To view metadata of Service click on URL of wsdl. You may notice that Service is hosted on localhost.

image

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.

  1. Using svcuitl.exe at command prompt
  2. 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.

image

You can consume service at client as following,

  1. Create instance of proxy class
  2. Call different operations from service

You can create instance of proxy class as following

image

And let us say you want to make a call to Add function. That can be done as following

image

So at client side you can call all four functions of Calculator Service as following


using System;
using ConsoleClient.ServiceReference1;

namespace ConsoleClient
{
 class Program
 {
 static void Main(string[] args)
 {

CalculatorClient proxy = new CalculatorClient();

double addResult = proxy.AddNumbers(9, 3);
 Console.WriteLine("Result of Add Operation");
 Console.WriteLine(addResult);
 double subResult = proxy.SubstractNumbers(9, 3);
 Console.WriteLine("Result of Substract Operation");
 Console.WriteLine(subResult);

double mulResult = proxy.MultiplyNumbers(9, 3);
 Console.WriteLine("Result of Multiply Operation");
 Console.WriteLine(mulResult);

double divResult = proxy.MultiplyNumbers(9, 3);
 Console.WriteLine("Result of Division Operation");
 Console.WriteLine(divResult);

Console.ReadKey(true);
 }
 }
}

Press F5 to run Console Client Application. You will get desired result.

image

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.

10 responses to “Four Steps to create first WCF Service: Beginners Series”

  1. […] Four Steps to create first WCF Service: Beginners Series (Dhananjay Kumar) […]

  2. […] Four Steps to create first WCF Service: Beginners Series | debug mode…… – […]

  3. Sandeep Singh Shekhawat

    Thanks sir to share this. It is so helpful and personally I want to read this type blog. It gives a confidence that I can learn WCF. Looking for your new blog. Thanks Sir

  4. Really it is good article …. but i got a error i can’t add service by giving address…So pls guide me where i went wrong…

  5. […] Four Steps to create first WCF Service: Beginners Series *** […]

  6. sir how to convert existing web services into restful wcf ?

  7. Step 2 seems to be giving me a bunch of problems can you post what the entire Web.config file is supposed to look like?

  8. Sir, How can make my service global, which will be accessible by any application.

  9. I have been hosted in IIS

  10. Amol Kumbhkarna

    Great Article Sir for the beginner like me in WCF … Thanks for sharing it…

Leave a comment

Create a website or blog at WordPress.com