Tuesday, 10 January 2012

Filled Under:

C#.Net How To: Create a WCF Service in Visual Studio 2010 C#.net

In my earlier articles, i have explained how to create web service in C#.net and then how to publish the web service in IIShow to consume web service

This C# tutorial explains how to create a WCF web service in visual studio 2010. The complete list of WCF tutorials is here.

Before proceeding make sure following things are installed in your system –
  1. Visual Studio 2010 (C#)
  2. Windows SDK’s
  3. IIS Windows features
    1. IIS Metabase and IIS 6 configuration utility
    2. IIS management console
    3. ASP.net

Steps to create a WCF service in Visual Studio 2010

  1. To create a new WCF service application project, go to menu File -> New -> Project. Then select WCF under Visual C# installed templates and then select WCF Service Application.
    WCF Service Application Creation
    WCF Service Application Creation
    Here I have specified the project name as “MyFirstWcfWebService”. Now, click on Ok button.
  2. If you look at the Solution Explorer, you will get following two code files added in web service project.
    1. Service1.svc
    2. IService1.cs. – This is an Interface. We call this interface as ServiceContract.
  3. You will get following auto generated code in Service1.svc.cs file –
    WCF Service - Auto Generated Code
    WCF Service - Auto Generated Code
    Here, GetData and GetDataUsingDataContract are the web methods. There is no harm in removing these two methods. I am keeping these two methods as it is and adding another method that accepts string and returns a wel come message.
  4. Add following code below GetDataUsingDataContract method –
    public String WelComeMessage(String name)
       return String.Format("{0}, Welcome to http://www.csharptutorial.in", name);
  5. Now open, IService1.cs from solution explorer. You will get following auto generated code –
    WCF Service - IService.cs Auto Generated Code
    WCF Service - IService.cs Auto Generated Code
    Here, we call Interface as ServiceContract and the corresponding methods are called as OperationContract.
  6. Now, we need to add OperationContract to our newly created Web method i.e WelComeMessage. Add following code below GetDataUsingDataContract
            String WelComeMessage(String name);

    After adding above, final code would look like -
    WCF Service - Final code
    WCF Service - Final code
  7. Your web service is ready. Now execute the web service by hitting F5 button. It will open following page.
    WCF Service - Directory Listing
    WCF Service - Directory Listing
  8. Click on Service1.svc. It will open following page.
    WCF Service - URL
    WCF Service - URL
To test this service, you will need to create a client and use it to call the service. Click here to know how to consume the WCF web service using client application. There is another way to test the web service. Click here to know how to test WCF test web service using wcftestclient.

One most important thing is to logg the WCF events and for messages for debugging purpose. Read more on how to enable WCF tracing and MessageLogging.

I hope you enjoyed the article. Could you please share the article on your social media? Thank you!

Following few articles might be of your interest -

Protected by Copyscape Plagiarism Check

Sharing is Caring...


  1. I liked your post....
    Keep it up.....

  2. Explained in very easy steps.. Nice articles..

  3. C# Tutorials is a life savoir, your blogs are very precised and easy to understand along with snap shot which ultimately saves the time and eases the task. Thanks

  4. Thanks............... but how to run WIndowsform which i hv added in console application Plzz help --- Mittal

  5. Thanks it helps me a lot. Your article is the best.

  6. Its a nice article for any begineer. please post some more articles which will be helpful to understand some more concepts in wcf.
    thanks a lot.

  7. very simple nice..