Jan
10
2012
C# // WCF

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

How to create a wcf service in visual studio 2010 is much talked topic on the internet. In this article we are going to see how to create wcf web service?. But first let’s understand what is WCF service?

What is WCF service?

Windows Communication Foundation (WCF) is a latest technology in web services category. Some people think it as a replacement for earlier web services like asmx (asp.net web service). WCF is a often use to develope and deploy network distributed services based on the principles of Service Oriented Architecture (SOA). The fundamental characteristic of WCF is Interoperability. This gives you more manageable approach to create and consume web services.

As compared to the asmx web services, WCF services gives you more reliability and security. WCF can do everthing and even more that ASMX (Asp.net web service) can do. WCF is much better than ASMX. Asp.net web services were designed to exchange SOAP messages over HTTP while WCF can exchange message as any format including SOAP and ReST. WCF is not limited to only HTTP protocol, it can make use of TCP, MSMQ or named pipe also. Please read here differences between asp.net web service and WCF service.

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

Watch Video : How to create and consume WCF 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 we start to create a wcf service in visual studio 2010 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 wcf service in visual studio 2010 we need to first 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
            [OperationContract]
            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.
    create a wcf service in visual studio 2010
    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 –

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

18 Comments+ Add Comment

  • I liked your post….
    Keep it up…..

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

  • 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

  • Excellent article…!

  • Thanks…………… but how to run WIndowsform which i hv added in console application Plzz help — Mittal

  • Thanks …:)

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

  • 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.

  • very nice

  • very nice..

  • Nice :)

  • nice Excellent

  • very simple nice..

  • Excellent

  • Excellent very easy steps

  • Thanks Buddy,
    your posts are all very helpful

    • Hi Alireza_N,
      Thanks for sharing your thoughts. :)

  • When i run a query got following error,.
    “The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.”

Leave a comment