in Uncategorized

A start point to work with WCF services

It’s about 3 months that I’m researching on WCF because of a reservation system I’m developing right now. We’re working on a web service which enables our customers to reserve cars, tickets, etc.; so we need something like XML Web Service to do such a thing.

WCF is a new framework for building service-oriented applications. Microsoft wanted to provide its developers with a framework to quickly get a proper service-oriented architecture up and running. Using the WCF, you will be able to take advantage of all the items that make distribution technologies powerful. WCF is the answer and the successor to all these other message distribution technologies.

In this post, I’m going to show you how to develop your first WCF application using Microsoft Visual Studio 2008.

This first step is to create a new WCF application. Visual Studio implemented a template for it so you can create it easier:

Creating a new WCF application - Photo taken by myself.

Note that WCF Service Applications are available in .NET Framework 3 and 3.5 so you cannot make a use of WCF when you’re developing applications using .NET Framework 2 or below!

When want to create a WCF application, you’ll need to follow some rules:

First Step

The first step is to create an interface and list your methods you want to use in your service:

public interface IService1
    string GetData(int value);

Everything is normal except that, you must use some attribute in order to create WCF service’s interface. First, you must apply an attribute named “ServiceContract” to your interface. Second, you have to apply another attribute named “OperationContract” to each of your methods you want to use in your WCF service. Note that you need to import System.ServiceModel name space to your interface in order to make use of “ServiceContract” and “OperationContract” attributes.

Second Step

Now it’s time to create our WCF service by adding a new WCF service to our application. The important part when you added a new WCF service to your application is that you must implement your WCF service class from the interface you’ve just created. After you created your service class, you can start writing codes in your specified methods:

public class Service1 : IService1
    public string GetData(int value)
        return string.Format("Here is your value: {0}", value); 

Third Step

When you create a new interface and class in a WCF application, you must apply some settings to your Web.Config file. Visual Studio automatically adds some lines of settings to your Web.Config file when you create a WCF application that contains the name of your interface and your service class.

Keep in mind that, you must update those file names in your Web.Config file if you want to change the name of that Service1.cs and Service1.svc. You can also add more interfaces and services if you want but you must apply them in Web.Config file:

  <service name="SampleService.Service1" 
    <endpoint address="" binding="wsHttpBinding" 
        <dns value="localhost"/>
    <endpoint address="mex" binding="mexHttpBinding" 
    <behavior name="SampleService.Service1Behavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>