C#.Net How To: Host a WCF Service in IIS 7 using C#.net VS 2010

This article explains the basic steps on how to host a wcf service in IIS. This article assumes that you have basic idea of wcf service like what is wcf service and how to create a wcf service?.

In my previous articles i have already explained how to create, publish and consume asp.net web services. In this wcf service tutorial let’s learn to host a WCF service in IIS .

Why to host a wcf service?

What is the use of your service if it is not exposed to any client? Hosting a wcf service somewhere gives any client an opportunity to consume it.

We can host asp.net web service in IIS only. Unlike traditional asp.net web service, WCF service can be hosted by many ways some are listed below. Each of these methods are having it’s own advantages and disadvantages.

  1. Host wcf service in IIS
  2. Host wcf service in Windows Service
  3. Self-host a wcf service
  4. Host wcf service in WAS (Windows Activation Service)

In this WCF tutorial we are going to focus only on method of hosting wcf service in IIS.

Why to host a WCF service in IIS?

Below points would help us to understand why to use IIS for WCF service hosting.

  1. IIS automatically launch the host process when it gets the first request from client.
  2. Some IIS features used such as Process Cycling, Idle Shutdown, Process Health Monitoring and Message Based Activation, etc.
  3. Main disadvantage – only support of Http protocol.

Before starting the actual IIS hosting in wcf, make sure that IIS 7 is installed in your machine and the .Net framework of your application pool should be set to v4.0.

Steps to create and host a WCF service in IIS 7

  1. Open VS 2010 and Goto File -> New -> Web Site..
    Create new WCF project
    Creating a new WCF project
  2. Select Visual C# template from left pane and WCF service from right pane. Make sure that .Net Framework 4 is selected.
    WCF Service - Project Selection
    WCF Service – Project Selection

    Then select HTTP in Web Location and enter your desired web name for the service. I have given MyFirstIISHostedWCFService name for this service.

  3. Click on Ok button to proceed. Following screen will appear.
    WCF Service - Auto generate code
    WCF Service – Auto generate code
  4. Here, I am creating one operation contract to return wel-come string. Click on IService interface and press F12 to go to the IService.cs file. Now write operation contract as shown in below image –
    WCF Service - OperationContract
    WCF Service – OperationContract
  5. Go to Service.cs file and append following code –
        public String Welcome(String name)
            return String.Format(“Welcome {0}”, name);

    See below screenshot for clear understanding –

    WCF Service - Create WebMethod
    WCF Service – Create WebMethod
  6. We have completed all steps to host a wcf service in IIS 7. Now press Ctrl+F5 to execute the WCF service. The service interface will open in your default web browser.
    Host a wcf service in IIS
    WCF Service hosted in IIS

    Your web service is now hosted in IIS 7 and ready to consume. Please read my previous post on How to consume a WCF service.

In this wcf tutotial we learned to host wcf service in IIS using very simple and basic steps. I hope you enjoyed the article. If you find this article helpful, then could you please share the article on your social media?

Related WCF articles …

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.

8 Comments+ Add Comment

  • Ashish vous êtes un épargnant de vie. Je viens de regarder pour les étapes et ne pouvait pas trouver n’importe où. et ici, j’ai eu toutes les étapes précisé.

    Merci Bro

  • Nice Post Ashish… Very easy to understand…

  • Very nice …

  • its great….. now my concept is clear about WCF .

  • A very nice article…
    can you please describe second method also to host a wcf web service in iis7

  • Wow… This is so easy to understand. Thank you so much

  • Nyc Post… Very Easier to Under Stand Even beginners Also….

  • It is very easy to understand and implement.
    Thanks for this post

Leave a comment