How to Define a WCF Service Contract

The first step required when creating a Windows Communication Foundation (WCF) service is to create a Contract for the it. The contract specifies what operations a service can perform. In normal programming terms operation can be conceived as method, so we can say operation is a method exposed in a web service. You can write code for web service in .Net compliant language like C#,VB.Net and C++.

Normally we need to create a interface to define the service contract and this contract contains the operations (methods) a service can perform.

Each interface must have the ServiceContract attribute associated with it and the each operation (method) with in the interface should have the OperationContract attribute associated with it. If any operation (method) without OperationContract attribute defined with in the interface that has the ServiceContract attribute, than that funciton will not be exposed to the outer world.

For practice we will create a Console application in the VS 2008 that will contain a interface to show you how to create a service contract. VS 2008 should be run as an administrator and after you create the console application you need to add the reference for System.ServiceModel.

When you create a console application than by default the solution has one file with the name of Program.cs and the code written in it would be like this

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

I have named the project as Service that’s why the namespace name is also Service.

After adding the reference for System.ServiceModel we also need to add one line of code on top of the file and that line will be a using statement as follows

Using System.ServiceModel;

This line will help the coders to use the classes with in this namespace without writing the full hierarchy of classes.

Now we will create a sample contract for a Web Service which will perform basic arithmetic function like Add,Subtract,Multiply and Division. So the code for this contract should be something like this:

using System;

// Add the using statment for System.ServiceModel 
using System.ServiceModel;
namespace Service
  // Define a service contract.
  public interface ICalculator
    // Create the method declaration for the contract.
    double Add(double d1, double d2);
    double Subtract(double d1, double d2);
    double Multiply(double d1, double d2);
    double Divide(double d1, double d2);

There are many parameter options available for both ServiceContract and OperationContract attribute. We can use them according to our need.

Compile the code and if there is no error than we are succeeded in creating our first Service Contract, which will be used in creating WCF Web Service.


2 thoughts on “How to Define a WCF Service Contract

Leave a Reply

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

You are commenting using your 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