Search This Blog


Create A Step by Step WCF Application

This is a getting started tutorial to Windows Communication Foundation(WCF) Service.The followinh application provide two services
-1.getName:Which will take two input string parameter first name and the last name and returns a string which is a concatination of both two input string
-2.getMessage:Which will take one input string parameter and returns the same string appending another string "Message :" as a prefix

I have used Microsoft Visual Web Developer 2008 Express Edition for creating service and Microsoft Visual C# 2008 Express Edition for creating a windows application to invoke that service.
So it is very Simple.Let's Start

Step 1.Create New WCF Service
Open Microsoft Visual Web Developer 2008 Express Edition to create a New Website
Go to File->New Web Site and you will see a template called WCF Service.Select this template and put "MyWCFService" as a File Name

Step 2.Define ServiceContract and OperationContract
Open the IService.cs under App_Code,Delete all lines of code except the namespace portion and copy the following code after namespace.
public interface IMyService
string getName(string fName, string lName);
string getMessage(string Message);

Step 3.Implement the Service
Open Service.cs under App_Code ,similarly delete all lines of code except the namespace.Create a new class MyNameService and implement IMyService.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

public class MyNameService : IMyService
#region IMyService Members

public string getName(string fName, string lName)
return fName + " " + lName;

public string getMessage(string Message)
return "Message :" + Message;

Step 4.Create Service Defination File(.SVC)
Open Service.svc change service to "MyNameService".
<%@ ServiceHost Language="C#" Debug="true" Service="MyNameService" CodeBehind="~/App_Code/Service.cs" %>

Step 5.Edit web.config file for service hosting
Go to web.config.Within system.serviceModel you have a services.Under services you will get service tag,
-Change the name to "MyNameService" which is a user defined class under Service.cs that impement IMyService.
-Change the behaviorConfiguration to MyNameServiceBehavior
Go to the end point tag within service tage,
-Change contract to IMyService which is the name of your interface.

Step 6.Enable metadata for client access
Within this web.config file go To serviceBehaviors under behaviors,Go To behavior tag
-Within the behavior tag change the name to MyNameServiceBehavior which you have mention in the behaviorConfiguration at step 5.
-Within serviceDebug tag set includeExceptionDetailInFaults=true

The system.serviceModel will look like as follows(Step 5 and Step 6)

<service name="MyNameService" behaviorConfiguration="MyNameServiceBehavior">
<endpoint address="" binding="wsHttpBinding" contract="IMyService"/>
<behavior name="MyNameServiceBehavior">
<!-- 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. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>

Step 7.Build and Test the application
Select Service.svc and right click.Click on "Set As Start Page"
Now run your application by clicking F5.You can see the following page when you run it.

Step 8.Generate a configuration file and a code file that contains the client class

Go to command prompt (type cmd in Start menu->run)

To go to the folder which contain SvcUtil.exe,Type
cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin

run the following command
svcutil.exe http://localhost:1805/MyWCFService/Service.svc?wsdl
You can get the url from the output window(web site) generated at step 7

After executing the command ,you will get following two files within the folder C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin or the folder which contain SvcUtil.exe


Step 9.Create A client application which will invoke your WCF Service
Create a windows application "MyWCFClient" in different .NET IDE

Step 10.Add Service Reference
Right click on your solution and click on "Add Service Reference"

Put the URL generated at step 7.
Click on GO.
Type "MyServiceReference" in a Namespace textbox.

Now Click OK
Now it will generate/Modify two files
-MyServiceReference under Service References folder

Step 11.Copy Configuratin and code file
Copy MyNameService.cs and output.config generated at Step 8. Paste it in the root folder of your windows application(MyWCFClient)

Step 12.Invoke the WCF Service
Call your WCF Service(MyService) in a button click event.Create an object of MyServiceClient and call two methods getName and getMessage that is declared in IMyService at step 2 and implemented in MyNameService at step 3

private void button1_Click(object sender, EventArgs e)
MyServiceClient msc = new MyServiceClient();
MessageBox.Show(msc.getName("Manab", "Basu"));

Step 13.Modify the app.config
Open the app.config and go to the endpoint tage within client under system.serviceModel.
Modify contract="MyServiceReference.IMyService" to contract="IMyService"

The file will look like as follows
<?xml version="1.0" encoding="utf-8" ?>
<binding name="WSHttpBinding_IMyService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<message clientCredentialType="Windows" negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true" />
<endpoint address="http://localhost:1805/MyWCFService/Service.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMyService"
contract="MyServiceReference.IMyService" name="WSHttpBinding_IMyService">
<userPrincipalName value="" />

Step 14.Run your client application
Run your windows application and clik on the button

So now you can see that the WCF is so simple :)

No comments: