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.

9 thoughts on “Four Steps to create first WCF Service: Beginners Series

  1. Pingback: Dew Drop – April 16, 2013 (#1,528) | Alvin Ashcraft's Morning Dew

  2. Pingback: My Latest Shared Bookmarksfboiton's blog | fboiton's blog

  3. 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. Pingback: Scott Banwart's Blog › Distributed Weekly 203

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s