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

  Switch on low bandwidth view
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). This topic assumes you are familiar with IIS and understand how to use the IIS management tool to create and manage IIS applications. For more information about IIS see Internet Information Services 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 more information about how WCF and ASP.NET interact, see WCF Services and ASP.NET. For more information about configuring security, see Windows Communication Foundation Security.

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. For more information about installing and configuring IIS see Installing and Configuring IIS 7.0

  2. Create a new folder for your application files called "IISHostedCalcService", 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. When creating an alias for the application directory use "IISHostedCalc".

  3. Create a new file named "service.svc" in the application directory. Edit this file by adding the following @ServiceHost element.

    <%@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 named service.cs in the App_Code subdirectory.

  6. Add the following using statements to the top of the service.cs file.

    using System;
    using System.ServiceModel;
  7. Add the following namespace declaration after the using statements.

    namespace Microsoft.ServiceModel.Samples
    {
    }
  8. Define the service contract inside the namespace declaration as shown in the following code.

    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);
    }
    
    
  9. Implement the service contract after the service contract definition as shown in the following code.

    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;
       }
    } 
    
    
  10. Create a file named "web.config" in the application directory and add the following configuration code into the file. At runtime, the WCF infrastructure uses the information to construct an endpoint that client applications can communicate with.

    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>
    
    
  11. To make sure the service is hosted correctly, open an instance of Internet Explorer and browse to the service's URL: http://localhost/IISHostedCalc/Service.svc

Example

The following is a complete listing of the code for the IIS hosted calculator service.

C#
using System;
using System.ServiceModel;

namespace Microsoft.ServiceModel.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;
     }
  } 

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>

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 ... Gahlaut   |   Edit   |   Show History

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   |   Show History
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
Change in .svc file      SJ1234 ... Thomas Lee   |   Edit   |   Show History

I had done something similar using notepad instead of Visual Studio and when I ran it in IIS, it wouldn't work until I changed
this:

<%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"%>

to

<%@ServiceHost language=c# Debug="true"
Service="Microsoft.ServiceModel.Samples.CalculatorService,Microsoft.ServiceModel.Samples "%>


I was getting this error:
The type <zzz> provided as the Service attribute value in the ServiceHost directive could not be found.

I had to put the syntax as Service = "namespace.class,namespace". Basically I had to add the name of namespace after comma.

It then worked in IIS. Hopefully this helps someone.

Tags What's this?: iis (x) service (x) wcf (x) Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker