WCF Part 3: Direct from WCF kitchen.

WCF
WCF @Image courtesy of ddpavumba/ FreeDigitalPhotos.net

WCF Part 3: Direct from WCF kitchen.

Welcome to CodeSpread!

In the last two articles WCF:Part 1 and WCF Part 2, we have covered basic knowledge and terminologies of WCF.Now we will apply that knowledge to create a WCF service.There are lot of articles available on internet related to creating a WCF service but the knowledge gap is still there. Let’s try to fill this gap today.

As a good chef, before making your WCF curry, please make sure you have following ingredients available in your kitchen.

  • Visual Studio 2008 or above
  • Windows SDK
  • IIS
  • .Net Framework 3.0 or above
  • WCF templates

We will follow these steps to create our WCF curry. In between, we will surely include our commentary to provide more insights.

  • Create a new Solution and Project.
  • Choose WCF Service Library Template.
  • Create the Service and Operation Contracts.
  • Implement Service and Operation Contracts.
  • Changes in App.Config.
  • Test the Service.

Create a new Solution and Project:

  1. Launch Visual Studio 2008.
  2. Click on File -> New –> Project
  3. “New Project” window will appear.NewProject
  4. On the Left hand side, Project types are available, choose WCF.WCFProject
  5. Here, we select WCF Service Library and provide the Name and Location to the project. We gave Name as “CalculatorService”.On Click of OK, We will have the solution and project created
    Note:There are a few built-in WCF service templates; two of them are Visual Studio WCF Service Library and Visual Studio WCF Service Application. In this article, we will use the service library template.Solution
  6. On the Right Hand side, we see “IService.cs” and “Service.cs” available. These are the default file created by the project with some default code in them. Please note that, if you rename these files, change the reference everywhere in the project or else solution will not compile.

Create Service Contract and Operation Contracts:

  1. Open IService.cs and same screen as above will appear. Here, we have to define service contracts and operations contracts. For more information about contracts, read WCF Part 2
  2. We have renamed “IService.cs” as “ICalculator.cs” and also replaced Service contracts with ICalculator. We have removed older Operation contracts and replaced them with new Operation contracts as Add(int num1, int num2) and Subtract(int num3,int num4) method definitions. As we are using .Net types so there is no need to define Data Contracts here. For more information about data contracts, read WCF Part 2.Contracts

Implement Service Contract and Operation Contracts:

  1. Rename Service.cs as something relevant like Calculator.cs . This file is used to implement contracts. I suggest looking at the code will be much more beneficial.Implementation

Now, build this solution. If the build is successful means there is no issues and service is ready to be served to the external world. But Before that, there are few modifications required to be done in App.Config.

Changes in App.Config:

  1. App.config contains endpoint details which are required, which includes ABC (Address, Binding and Contract).For more information about ABC, read WCF Part 2.
    • Open the App.config file.
    • <xml version="1.0" encoding="utf-8" ?>
      <configuration>
      <system.web>
      <compilation debug="true" />
      </system.web>
      <!-- When deploying the service library project, the content of the config file must be added to the host's -->
      app.config file. System.Configuration does not support config files for libraries. -->
      <system.serviceModel>
      <services>
      <service name="CalculatorService.Service1" behaviorConfiguration="CalculatorService.Service1Behavior">
      <host>
      <baseAddresses>
      baseAddress = "http://localhost:8731/Design_Time_Addresses/CalculatorService/Service1/" />
      </baseAddresses>
      </host>
      <!-- Service Endpoints -->
      <!-- Unless fully qualified, address is relative to base address supplied above -->
      <endpoint address ="" binding="wsHttpBinding" contract="CalculatorService.IService1">
      <!--
      Upon deployment, the following identity element should be removed or replaced to reflect the
      identity under which the deployed service runs.&nbsp; If removed, WCF will infer an appropriate identity
      automatically.
      -->
      <identity>
      <dns value="localhost"/>
      </identity>
      </endpoint>
      <!-- Metadata Endpoints -->
      <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
      <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
      </services>
      <behaviors>
      <serviceBehaviors>
      <behavior name="CalculatorService.Service1Behavior">
      <!-- To avoid disclosing metadata information,
      set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="True"/>
      <!-- To receive exception details in faults for debugging purposes,
      set the value below to true.&nbsp; Set to false before deployment
      to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="False" />
      </behavior>
      </serviceBehaviors>
      </behaviors>
      </system.serviceModel>
      </configuration>
      
    • Change all instances of the Service1 to Calculator and IService to ICalculator.
    • You can also change Behaviour to any other Behaviour settings.
    • Once Changed, App.Config will look like
    • <xml version="1.0" encoding="utf-8" ?>
      <configuration>
      <system.web>
      <compilation debug="true" />
      </system.web>
      <!-- When deploying the service library project, the content of the file must be added to the host's -->
      app.config file. System.Configuration does not support config files for libraries. -->
      <system.serviceModel>
      <services>
      <service name="CalculatorService.Calculator" behaviorConfiguration="CalculatorService.CalculatorBehavior">
      <host>
      <baseAddresses>
      baseAddress = "http://localhost:8731/Design_Time_Addresses/CalculatorService/Calculator/" />
      </baseAddresses>
      </host>
      <!-- Service Endpoints -->
      <!-- Unless fully qualified, address is relative to base address supplied above -->
      <endpoint address ="" binding="wsHttpBinding" contract="CalculatorService.ICalculator">
      <!--
      Upon deployment, the following identity element should be removed or replaced to reflect the
      identity under which the deployed service runs.&nbsp; If removed, WCF will infer an appropriate identity
      automatically.
      -->
      <identity>
      <dns value="localhost"/>
      </identity>
      </endpoint>
      <!-- Metadata Endpoints -->
      <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
      <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
      </services>
      <behaviors>
      <serviceBehaviors>
      <behavior name="CalculatorService.CalculatorBehavior">
      <!-- To avoid disclosing metadata information,
      set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="True"/>
      <!-- To receive exception details in faults for debugging purposes,
      set the value below to true.&nbsp; Set to false before deployment
      to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="False" />
      </behavior>
      </serviceBehaviors>
      </behaviors>
      </system.serviceModel>
      </configuration>
      

Test the Service:

  1. WCF service can be tested by WCF Test Client. Just press F5 and Test client will open. The behavior of Test client will be same as any other real-time client.TestClient
  2. You can see two of our operation exposed by the service to the client.Click on Add(). Just enter value for num1 and num2 in Request window and press Invoke.TestService
  3. In the Response window, outcome of the Service will be displayed. It is also available in XML format.

In this article, we learned how to create a WCF service and test it with the test client. In the next article, we will learn how to host the service and test it with the real-time clients. There are quite a good options available to host the WCF service like Self-Hosting, IIS, WAS, Console Application, among all of them IIS is the most popular one. We will create a client and consume the WCF service also.

In the end, Our first WCF curry is ready and served HOT!!

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • WCF Part 2: Fundamentals.WCF Part 2: Fundamentals.We have seen the evolution of WCF in our previous article: WCF Part 1: Why WCF?. Next, we will go through the fundamentals of WCF. I will try to make it more worthy than an interview question puff....
  • WCF Part 1: Why WCF?WCF Part 1: Why WCF?When i create a WCF service, i feel like it is a very easy task but when somebody asks me, why you have created it? then the simplest answer is, i got the requirement like this. But to be honest, N...
  • Few lines about WCF metadataFew lines about WCF metadataWhat is WCF metadata? WCF generates metadata for service endpoints and this metadata is used to describe how to interact with the service's endpoints. This information is used by Svcutil.exe to ge...
  • Caching in WCF ServicesCaching in WCF ServicesYes, It is possible and clearly a  significant performance enhancement. How caching works for WCF services? Lets assume, a user sends a request to a service so at server, service method is called...
  • REST- REpresentational State TransferREST- REpresentational State TransferREST – REpresentational State Transfer, is an enhanced version of client server style architecture for distributed communication. When we talk about distributed communication, we think of reques...
  • Difference Between Transport and Message Security in WCFDifference Between Transport and Message Security in WCFTransport Security in WCF As the name suggests, it is concerned with security of communication between client and the service over a network protocol. It guarantees the confidentiality and integri...
  • SQL:My first SSIS PackageSQL:My first SSIS PackageI would like to share my experience about a situation, where I was given a task to export a file(say XML) from FTP location and get the data from this file populated into our database tables. Moreo...
  • Try one ORM: Entity FrameworkTry one ORM: Entity FrameworkAs promised in the last article, We are going to share our experience of Entity Framework Ver 4.1. First of all, we have to add and configure Entity Framework in visual studio. We are going to u...