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.

    Visual Basic
    <ServiceContract()> _
    Public Interface ICalculator
    
        <OperationContract()> _
          Function Add(ByVal n1 As Double, _
                       ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Subtract(ByVal n1 As Double, _
                          ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Multiply(ByVal n1 As Double, _
                          ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Divide(ByVal n1 As Double, _
                        ByVal n2 As Double) As Double
    End Interface
    
    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.

    Visual Basic
    Public Class CalculatorService
        Implements ICalculator
        Public Function Add(ByVal n1 As Double, _
                            ByVal n2 As Double) As Double Implements ICalculator.Add
            Return n1 + n2
        End Function
    
        Public Function Subtract(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Return n1 - n2
        End Function
    
        Public Function Multiply(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Return n1 * n2
        End Function
    
        Public Function Divide(ByVal n1 As Double, _
                               ByVal n2 As Double) As Double Implements ICalculator.Divide
            Return n1 / n2
        End Function
    End Class
    
    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.

Visual Basic
Imports System
Imports System.ServiceModel

Namespace Microsoft.ServiceModel.Samples

    <ServiceContract()> _
    Public Interface ICalculator

        <OperationContract()> _
          Function Add(ByVal n1 As Double, _
                       ByVal n2 As Double) As Double

        <OperationContract()> _
        Function Subtract(ByVal n1 As Double, _
                          ByVal n2 As Double) As Double

        <OperationContract()> _
        Function Multiply(ByVal n1 As Double, _
                          ByVal n2 As Double) As Double

        <OperationContract()> _
        Function Divide(ByVal n1 As Double, _
                        ByVal n2 As Double) As Double
    End Interface


    Public Class CalculatorService
        Implements ICalculator
        Public Function Add(ByVal n1 As Double, _
                            ByVal n2 As Double) As Double Implements ICalculator.Add
            Return n1 + n2
        End Function

        Public Function Subtract(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Subtract
            Return n1 - n2
        End Function

        Public Function Multiply(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements ICalculator.Multiply
            Return n1 * n2
        End Function

        Public Function Divide(ByVal n1 As Double, _
                               ByVal n2 As Double) As Double Implements ICalculator.Divide
            Return n1 / n2
        End Function
    End Class
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.
Build Date: 2009-10-13
Tags :


Community Content

Gahlaut
Using VS 2008 Project Template

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 :

C.R. Hinners
Clarification on the usage of an interface in the above sample
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 :

Thomas Lee
Change in .svc file

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 : service wcf iis

dimwit
IIS 7 Hosting Info
If you want to host in IIS 7 and are getting lot's of errors, be sure to check out http://msdn.microsoft.com/en-us/library/ms752252.aspx.
Tags : wcf iis7

Than1
IIS Hosting with compiled assemblies
<%@ServiceHost language=c# Debug="true"
Service="Microsoft.ServiceModel.Samples.CalculatorService,Microsoft.ServiceModel.Samples "%>

this will work when using compiled assemblies. Dropping the assemblies in a bin folder inside the application folder and then changing the ServiceHost directive as above will use the compiled code.


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

will work with the source files in app_code folder. code gets compiled on demand.
Tags : iis wcfhosting

Roonster
Change to web.config

The code above didn't work for me until I replaced the web.config with the following:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="mexBehavior" name="Microsoft.ServiceModel.Samples.CalculatorService">
<endpoint address="http://localhost:8020/Service.svc" binding="basicHttpBinding"
bindingConfiguration="" contract="Microsoft.ServiceModel.Samples.ICalculator" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>




The endpoint address of http://localhost:8020/Service.svc is the location of the service or I believe can be left blank if the service is running as the default localhost web site. The key thing is that the behaviors section was missing from the web.config in the example code and so the example code would not work.


Page view tracker