Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Hosting Services
 How to: Host a WCF Service in IIS
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
How to: Host a WCF Service in IIS

This topic outlines the basic steps required to create a Windows Communication Foundation (WCF) service that is hosted in Internet information Services (IIS). A WCF service that runs in the IIS environment takes full advantage of IIS features, such as process recycling, idle shutdown, process health monitoring, and message-based activation. This hosting option requires that IIS be properly configured, but it does not require that any hosting code be written as part of the application. You can use IIS hosting only with an HTTP transport.

For the source copy of this example, see IIS Hosting Using Inline Code.

To create a service hosted by IIS

  1. Confirm that IIS is installed and running on your computer.

  2. Create a new folder for your application files, ensure that ASP.NET has access to the contents of the folder, and use the IIS management tool to create a new IIS application that is physically located in this application directory.

  3. Create a new file named "service file" with an .svc extension in the application. Edit this file by adding the appropriate @ServiceHost directive information for the service. For example, the service file contents for the CalculatorService samples contain the following information.

    <%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"%>
  4. Create an App_Code subdirectory within the application directory.

  5. Create a code file in the App_Code subdirectory.

  6. Define the service contract for the type of service in the code file.

    C#
    [ServiceContract]
    public interface ICalculator
    {
       [OperationContract]
       double Add(double n1, double n2);
       [OperationContract]
       double Subtract(double n1, double n2);
       [OperationContract]
       double Multiply(double n1, double n2);
       [OperationContract]
       double Divide(double n1, double n2);
    }
    
    
  7. Implement the service contract in a service class in the code file.

    C#
    public class CalculatorService : ICalculator
    {
       public double Add(double n1, double n2)
       {
          return n1 + n2;
       }
       public double Subtract(double n1, double n2)
       {
          return n1 - n2;
       }
       public double Multiply(double n1, double n2)
       {
          return n1 * n2;
       }
       public double Divide(double n1, double n2)
       {
          return n1 / n2;
       }
    } 
    
    
  8. Create a file named "Web.config" in the application directory. To run the service, the Web.config file must be located in the same directory as the service file.

  9. Add the appropriate configuration code into the file. At runtime, the WCF infrastructure uses the information to construct an endpoint that client applications can communicate with. For the CalculatorService sample, the configuration code is in the following example.

    Xml
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="Microsoft.ServiceModel.Samples.CalculatorService">
    
            <!-- This endpoint is exposed at the base address provided by host:                                        http://localhost/servicemodelsamples/service.svc  -->
            <endpoint address=""
                      binding="wsHttpBinding"
                      contract="Microsoft.ServiceModel.Samples.ICalculator" />
    
            <!-- The mex endpoint is explosed at http://localhost/servicemodelsamples/service.svc/mex -->
            <endpoint address="mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange" />
          </service>
        </services>
      </system.serviceModel>
    
    </configuration>
    
    

Example

The following code shows the contents of the code file with the contract and its implementation within the namespace specified by the directive in the Service.svc file.

C#
using System;
using System.ServiceModel;

namespace Samples
{

  [ServiceContract]
  public interface ICalculator
  {
     [OperationContract]
     double Add(double n1, double n2);
     [OperationContract]
     double Subtract(double n1, double n2);
     [OperationContract]
     double Multiply(double n1, double n2);
     [OperationContract]
     double Divide(double n1, double n2);
  }


  public class CalculatorService : ICalculator
  {
     public double Add(double n1, double n2)
     {
        return n1 + n2;
     }
     public double Subtract(double n1, double n2)
     {
        return n1 - n2;
     }
     public double Multiply(double n1, double n2)
     {
        return n1 * n2;
     }
     public double Divide(double n1, double n2)
     {
        return n1 / n2;
     }
  } 

See Also


© 2007 Microsoft Corporation. All rights reserved.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Using VS 2008 Project Template      LyalinDotCom   |   Edit   |  

Another way to create a sample service like this is to use the Visual Studio 2008 project template. Here is a blog post with more deatil:

http://blog.lyalin.com/2008/04/creating-quick-sample-iis-hosted-wcf.html

Tags What's this?: Add a tag
Flag as ContentBug
Clarification on the usage of an interface in the above sample      C.R. Hinners   |   Edit   |  
The sample in this article could lead one to think that an interface is required in order to implement a service; this is not the case. The sample could have been written without the ICalculator interface by applying the ServiceContract attribute to the CalculatorService class and the OperationsContract attribute to the methods. The decision to use an interface in the sample appears to be a stylistic one by the authors; nothing else should be read into it as far as implementing services is concerned.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker