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
-
Confirm that IIS is installed and running on your computer.
-
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.
-
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"%>
-
Create an App_Code subdirectory within the application directory.
-
Create a code file in the App_Code subdirectory.
-
Define the service contract for the type of service in the code file.
[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);
}
-
Implement the service contract in a service class in the code file.
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;
}
}
-
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.
-
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 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.
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.