How to: Host a WCF Service in IIS - WCF (2024)

  • Article

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 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 directives to the top of the Service.cs file.

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

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

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

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

    <?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <services> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviors"> <!-- 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 exposed at http://localhost/servicemodelsamples/service.svc/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehaviors"> <!-- Add the following element to your service behavior configuration. --> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>

    This example explicitly specifies endpoints in the configuration file. If you do not add any endpoints to the service, the runtime adds default endpoints for you. For more information about default endpoints, bindings, and behaviors see Simplified Configuration and Simplified Configuration for WCF Services.

  11. To make sure the service is hosted correctly, open a browser 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.

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; } }}
Imports System.ServiceModelNamespace 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
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <services> <service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviors"> <!-- 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 exposed at http://localhost/servicemodelsamples/service.svc/mex --> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehaviors"> <!-- Add the following element to your service behavior configuration. --> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration>

See also

  • Hosting in Internet Information Services
  • Hosting Services
  • WCF Services and ASP.NET
  • Security
  • Windows Server App Fabric Hosting Features
How to: Host a WCF Service in IIS - WCF (2024)

FAQs

How to host WCF service library in IIS? ›

IIS Hosting of WCF Service
  1. First let's create a simple WCF Service. ...
  2. Open Visual Studio and select "File" -> "New" -> "Project...". ...
  3. Delete the Iservice1. ...
  4. Then add the two files IProduct. ...
  5. In the ProductService. ...
  6. Now make the following changes in the App. ...
  7. As you can see we are using wsHttpBinding. ...
  8. Now run the project.
Apr 19, 2015

How to activate WCF service in IIS? ›

Activate Windows Communication Foundation (WCF)
  1. From the Start menu, select Administrative Tools > Server Manager.
  2. Select Add roles and features from the Dashboard.
  3. Select Next twice.
  4. Select Features.
  5. In the Features area, expand the: - . ...
  6. Under WCF Services select: - HTTP Activation.

How many ways can we host WCF service? ›

There are three types of hosting environments for WCF services: IIS, WAS, and self-hosting. The term “self-hosting” refers to any application that provides its own code to initialize the hosting environment. This includes console, Windows Forms, WPF, and managed Windows services.

How do I deploy a WCF service? ›

Open “MathService” WCF Service project with administrator rights (Run as Administrator) and then, add new web site (MathService_IIS) to its solution, as shown below. Now, add reference of the WCF Service to IIS Host project. Make the following changes in 'Service. svc' file.

How to run a WCF service on localhost? ›

To open WCF Test Client, open Developer Command Prompt for Visual Studio and execute WcfTestClient.exe. Select Add Service from the File menu. Type http://localhost:8080/hello into the address box and click OK. Make sure the service is running or else this step fails.

How do I add a WCF service reference to my class library? ›

Add a WCF service reference
  1. In Solution Explorer, right-click the name of the project to which you want to add the service, and then select Add Service Reference. ...
  2. In the Address box, enter the URL for the service, and then select Go to search for the service.
Jul 29, 2024

Does WCF need IIS? ›

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.

How do I invoke WCF service? ›

With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button. The dialog will display a list of services available at the address you specify.

How do you check WCF service is running or not? ›

Testing WCF service using Test Client (wcftestclient.exe)
  1. Open Visual Studio command prompt and type wcftestclient then Enter.
  2. After pressing Enter, we get a GUI application of the WCF Test Client.
  3. Select File->Add Service and type the service endpoint url as address and click ok. ...
  4. Double click on Greeting Method.
Feb 14, 2011

What are the 3 things that a WCF services endpoint must have? ›

The three major points in WCF are the address, contract, and binding. The address defines the location of the services. The contract specifies the interface between the client and the service. And the binding defines how the two parties, the client and the service, will communicate with each other.

Is WCF service synchronous or asynchronous? ›

Windows Communication Foundation (WCF) services and clients can participate in asynchronous operation calls at two distinct levels of the application, which provide WCF applications even more flexibility to maximize throughput balanced against interactivity.

Where is the WCF service hosted? ›

Self-Hosting − When a WCF service is hosted in a managed application, it is known as self-hosting. It requires a developer to write the requisite coding for ServiceHost initialization. In self-hosting, a WCF service can be hosted in a variety of applications like Console application, Windows form, etc.

How to enable WCF service in IIS? ›

In this article
  1. Ensure that IIS, ASP.NET, WCF, and the WCF activation component are correctly installed and registered.
  2. Create a new IIS application, or reuse an existing ASP.NET application.
  3. Create a . svc file for the WCF service.
  4. Deploy the service implementation to the IIS application.
  5. Configure the WCF service.
Sep 15, 2021

How to host a service in IIS? ›

If PPASS is already installed skip to step 3.
  1. Step 1: Copy your Application files to the IIS Machine. ...
  2. Step 2: Install your Application on IIS. ...
  3. Step 3: Enable IIS Feature. ...
  4. Step 4: Add IIS Features. ...
  5. Step 5: Create a New IIS Site. ...
  6. Step 6: Configure the IIS Site. ...
  7. Step 7: Configure the IIS Application Pool User.
Feb 18, 2024

How do I create a WCF service? ›

Also, a basic understanding of C# will be helpful.
  1. Step 1: Creating a new WCF Service Application. First, open Visual Studio and create a new project. ...
  2. Step 2: Defining the Service Contract. ...
  3. Step 3: Implementing the Service Contract. ...
  4. Step 4: Configuring the Service. ...
  5. Step 5: Hosting and Testing the Service.

Which IIS version supports hosting of WCF service in Windows Activation Service? ›

WCF services hosted in IIS 5.1 and IIS 6.0 are restricted to using HTTP-based communication. On these IIS platforms, configuring a hosted service to use a non-HTTP binding results in an error during service activation. For IIS 7.0, the supported transports include HTTP, Net. TCP, Net.

How to host net TCP WCF service? ›

The following is the procedure to host a WCF service in a Windows Service using TCP.
  1. Step 1: Create WCF Service. ...
  2. Step 2: Configure WCF Service Endpoints to use TCP. ...
  3. Step 3: Create Windows service. ...
  4. Step 4: Add the Service Installers to the Windows Service. ...
  5. Step 5: Modify the Windows Service to host the WCF Service.
Sep 17, 2019

How do I host a WCF service in console application? ›

Create Simple WCF Service And Host It On Console Application
  1. Open Visual Studio instance with "Run as administrator" and create one class library project. ...
  2. Now, right click on the project and add a new item. ...
  3. It will generate 2 files - interface and class file.
  4. Now, open the interface file and add a new function.
Dec 16, 2016

Top Articles
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 5714

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.