Reader Level:
ARTICLE

Four Steps to Create First WCF Service For Beginners

On April 16, 2013 Articles | WCF with C#
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.
  • 8
  • 0
  • 82958

This article is for beginners who are starting in WCF. I will focus on helping you to create and consume your first WCF Service in the simplest of steps. The following is the procedure.

WCF1.jpg

At the end of this article you should able to create your first WCF Service and consume that in a Console Application. I will continue to add further articles 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 the project and select "Add" | "New Item..."
  2. From the Web tab choose WCF Service to add.
  3. Let us give the service the name "Calculator.svc".

WCF2.jpg

Next you need to perform the following tasks:

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

You will create an ICalculator Service Contract with the four basic calculator operations as the 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);

    }

}

You have created a Service Contract above with four Operation Contracts. These contracts will be part of the Service Contract and exposed to clients. By this step you have created the ICalculator Service Contract.

Step 2: Expose Endpoints with Metadata

In this step you need to expose Endpoints and the Metadata of the service. To do this open the Web.config file. We are going to create one Endpoint with basicHttpBinding. We are adding a metadata Endpoint also to expose the metadata of the service. We need metadata at the client side to create a 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 the service. To implement the service, open Calculator.svc and provide code to implement the functions defined in ServiceContract ICalculator.cs. Delete the implementation of the DoWork function from Calculator.svc and implement the services as in the following.

Calculator.svc.cs

namespace fourstepblogdemo

{

   

    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 the Service and configured the Endpoint. Now you need to host the service. There are many processes in which a WCF Service can be hosted. Some processes are:

  1. Managed Application

  2. IIS

  3. ASP.Net Web Server

  4. Windows Service

  5. App Fabric

In this article we are not going into the details of the WCF Service hosting and we will consider the simplest hosting option. Let us host the service in an ASP.Net Web Server. To host it press F5 in Visual Studio.
In the browser you will see the Service as in the following.

WCF3.jpg

To view the metadata of the Service click on the WSDL URL. You may notice that the Service is hosted by "localhost".

WCF4.jpg

Step 3: Consume Service

There are various ways a WCF SOAP Service can be consumed in various kinds of clients. In this article we will consume the service in a Console Application. Launch Visual Studio and create a Console Application project.

Now there are two ways you can create a proxy at the client side.

  1. Using svcuitl.exe at command prompt

  2. By adding Service Reference

In this article we will create a proxy at the client side using Add Service Reference. In the Console Project right-click on References and select the "Add Service Reference" option. In the Add Service Reference dialog copy and paste the address of the Service or if the WCF Service and Console Client project is in the same solution then click on Discover. If there is no error in the Service then you will find the Service Reference added as given in the following image. If you want you can change the name of the Reference. I am leaving here the default name "ServiceReference1".

WCF5.jpg

You can consume the service by the client as in the following:

  1. Create instance of proxy class

  2. Call different operations from service

You can create an instance of the proxy class as in the following:

WCF6.jpg

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

WCF7.jpg

So at the client side you can call all four of the functions of the Calculator Service as in the 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 the Console Client Application. You will get the desired results.

WCF8.jpg

You have now created a Calculator WCF Service and consumed that in a Console Application. In further articles I will simplify other concepts for WCF for beginners. I hope you find this article useful. Thanks for reading.

Article Extensions
Contents added by Suthish Nair on Apr 17, 2013

Those are using VS2010, here given below the web.config updates..

 <?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WcfService1.Calculator">
<endpoint address="" contract="WcfService1.ICalculator" binding="basicHttpBinding"/>
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
</service>
</services>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>

Here WcfService1 is my solution name.

  • 5rank
  • 7M readers
  • 3times
COMMENT USING

Trending up