Deploying an Internet Information Services-Hosted WCF Service - WCF (2024)

  • Article

Developing and deploying a Windows Communication Foundation (WCF) service that is hosted in Internet Information Services (IIS) consists of the following tasks:

  • Ensure that IIS, ASP.NET, WCF, and the WCF activation component are correctly installed and registered.

  • Create a new IIS application, or reuse an existing ASP.NET application.

  • Create a .svc file for the WCF service.

  • Deploy the service implementation to the IIS application.

  • Configure the WCF service.

For a detailed walkthrough of creating an IIS-hosted WCF service, see How to: Host a WCF Service in IIS.

Ensure That IIS, ASP.NET and WCF Are Correctly Installed and Registered

WCF, IIS, and ASP.NET must be installed for IIS-hosted WCF services to function correctly. The procedures for installing WCF (as part of the .NET Framework), ASP.NET, and IIS vary depending on your operating system. For more information about installing WCF and the .NET Framework, see Install the .NET Framework for developers. To install IIS on Windows 10, open Programs and Features in Control Panel and then select Turn Windows features on or off. In Windows Features, select Internet Information Services and then choose OK.

Deploying an Internet Information Services-Hosted WCF Service - WCF (1)

Instructions for installing IIS on other operating systems can be found at Install IIS on Windows Vista and Windows 7 and Install IIS 8.5 on Windows Server 2012 R2.

The installation process for .NET Framework automatically registers WCF with IIS if IIS is already present on the machine. If IIS is installed after .NET Framework, an additional step is required to register WCF with IIS and ASP.NET. You can do this as follows, depending on your operating system:

  • Windows 7 and Windows Server 2003: Use the ServiceModel Registration Tool (ServiceModelReg.exe) tool to register WCF with IIS. To use this tool, enter ServiceModelReg.exe /i /x in Visual Studio Developer Command Prompt or Visual Studio Developer PowerShell.

  • Windows 7: Finally, you must verify that ASP.NET is configured to use .NET Framework version 4 or later. You do this by running the ASPNET_Regiis tool with the –i option. For more information, see ASP.NET IIS Registration Tool.

Create a New IIS Application or Reuse an Existing ASP.NET Application

IIS-hosted WCF services must reside inside of an IIS application. You can create a new IIS application to host WCF services exclusively. Alternatively, you can deploy an WCF service into an existing application that is already hosting ASP.NET 2.0 content (such as .aspx pages and ASP.NET Web services [ASMX]). For more information about these options, see the "Hosting WCF Side-by-Side with ASP.NET" and "Hosting WCF Services in ASP.NET Compatibility Mode" sections in WCF Services and ASP.NET.

Note that IIS 6.0 and later versions periodically restart an isolated object-oriented programming application. The default value is 1740 minutes. The maximum value supported is 71,582 minutes. This restart can be disabled. For more information about this property, see the PeriodicRestartTime.

Create an .svc File for the WCF Service

WCF services hosted in IIS are represented as special content files (.svc files) inside the IIS application. This model is similar to the way ASMX pages are represented inside of an IIS application as .asmx files. A .svc file contains a WCF-specific processing directive (@ServiceHost) that allows the WCF hosting infrastructure to activate hosted services in response to incoming messages. The most common syntax for a .svc file is in the following statement.

<% @ServiceHost Service="MyNamespace.MyServiceImplementationTypeName" %>

It consists of the @ServiceHost directive and a single attribute, Service. The value of the Service attribute is the common language runtime (CLR) type name of the service implementation. Using this directive is basically equivalent to creating a service host using the following code.

new ServiceHost( typeof( MyNamespace.MyServiceImplementationTypeName ) );

Additional hosting configuration, such as creating a list of base addresses for the service can also be done. You can also use a custom ServiceHostFactory to extend the directive for use with custom hosting solutions. The IIS applications that host WCF services are not responsible for managing the creation and lifetime of ServiceHost instances. The managed WCF hosting infrastructure creates the necessary ServiceHost instance dynamically when the first request is received for the .svc file. The instance is not released until either it is closed explicitly by code or when the application is recycled.

For more information about the syntax for .svc files, see @ServiceHost.

Deploy the Service Implementation to the IIS Application

WCF services hosted in IIS use the same dynamic compilation model as ASP.NET 2.0. Just as with ASP.NET, you can deploy the implementation code for IIS-hosted WCF services in several ways at various locations, as follows:

  • As a precompiled .dll file located in the global assembly cache (GAC) or in the application’s \bin directory. Precompiled binaries are not updated until a new version of the class library is deployed.

  • As uncompiled source files located in the application’s \App_Code directory. Source files located in this directory are dynamically required when processing the application’s first request. Any changes to files in the \App_Code directory cause the entire application to be recycled and recompiled when the next request is received.

  • As uncompiled code placed directly in the .svc file. Implementation code can also be located inline in the service’s .svc file, after the @ServiceHost directive. Any changes to inline code cause the application to be recycled and recompiled when the next request is received.

For more information about the ASP.NET 2.0 compilation model, see ASP.NET Compilation Overview.

Configure the WCF Service

IIS-hosted WCF services store their configuration in the applications Web.config file. IIS-hosted services use the same configuration elements and syntax as WCF services hosted outside of IIS. However, the following constraints are unique to the IIS hosting environment:

  • Base addresses for IIS-hosted services.

  • Applications hosting WCF services outside of IIS can control the base address of the services they host by passing a set of base address URIs to the ServiceHost constructor or by providing a <host> element in the service’s configuration. Services hosted in IIS do not have the ability to control their base address; the base address of an IIS-hosted service is the address of its .svc file.

Endpoint Addresses for IIS-Hosted Services

When hosted in IIS, endpoint addresses are always considered to be relative to the address of the .svc file that represents the service. For example, if the base address of a WCF service is http://localhost/Application1/MyService.svc with the following endpoint configuration:

<endpoint address="anotherEndpoint" />

This provides an endpoint that can be reached at http://localhost/Application1/MyService.svc/anotherEndpoint.

Similarly, the endpoint configuration element that uses an empty string as the relative address provides an endpoint reachable at http://localhost/Application1/MyService.svc, which is the base address.

<endpoint address="" />

You must always use relative endpoint addresses for IIS-hosted service endpoints. Supplying a fully-qualified endpoint address (for example, http://localhost/MyService.svc) can lead to errors in the deployment of the service if the endpoint address does not point to the IIS-application that hosts the service exposing the endpoint. Using relative endpoint addresses for hosted services avoids these potential conflicts.

Available Transports

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.Pipe, Net.MSMQ, and msmq.formatname for backwards compatibility with existing MSMQ applications.

HTTP Transport Security

IIS-hosted WCF services can make use of HTTP transport security (for example, HTTPS and HTTP authentication schemes such as Basic, Digest, and Windows Integrated Authentication) as long as the IIS virtual directory that contains the service supports those settings. The HTTP Transport Security settings on a hosted endpoint’s binding must match the transport security settings on the IIS virtual directory that contains it.

For example, a WCF endpoint configured to use HTTP digest authentication must reside in an IIS virtual directory that is also configured to allow HTTP digest authentication. Unmatched combinations of IIS settings and WCF endpoint settings result in an error during service activation.

See also

  • Hosting in Internet Information Services
  • Internet Information Services Hosting Best Practices
  • Windows Server App Fabric Hosting Features
Deploying an Internet Information Services-Hosted WCF Service - WCF (2024)

FAQs

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.

What is hosting WCF service? ›

After creating a WCF service, the next step is to host it so that the client applications can consume it. This is known as WCF service hosting. A WCF service can be hosted by using any of the four ways given below − IIS Hosting − IIS stands for Internet Information Services.

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.

What is WCF and how does it work? ›

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application.

How do I activate WCF service? ›

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 do I use WCF service? ›

Consuming WCF Service Hosted in IIS 5/6
  1. Step 1 − Once a service is hosted in IIS, we have to consume it in client applications. ...
  2. Step 2 − Now, we will start creating the Console application using Visual Studio 2008 (Client application).
  3. Step 3 − Add the reference 'System. ...
  4. Step 4 − Create a Proxy class.

Is a WCF service an API? ›

WCF is used for SOAP-based service development, whereas Web API is utilized for both SOAP-based and RESTful service development. WCF does not provide support for MVC functionalities, although Web API does. WCF supports HTTP, UDP, and custom transport protocols, whereas Web API only supports HTTP.

What is the difference between WCF service and Web service? ›

Web services support only one protocol- HTTP and HTTPS during communication, but WCF supports more protocols like- HTTP, TCP, and MSMQ that can be extended for a comprehensive solution, reliable session, and transactions. It signifies WCF is more adaptable to work together for a variety of software.

How many ways we can 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.

Do people still use WCF? ›

As of the release of . NET 5, WCF is basically deprecated. While it will still work with . NET Framework for years to come, it won't be supported in .

How to build a WCF service? ›

Creating a WCF Service

You can do this by opening Visual Studio, clicking on File > New > Project. Then, select the 'WCF' option and click on 'WCF Service Library'. The code above defines a WCF service with a single endpoint. The 'binding' attribute specifies how the service can be accessed.

What is the goal of WCF? ›

WCF unified all these into a single service development framework with the following goals: Provide a unified programming model for building services across transport protocols like HTTP, TCP, Named Pipes etc. Enable services to expose multiple endpoints and bindings simultaneously.

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

How to run WCF service in local? ›

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.

Can WCF be deployed with Azure? ›

Migrating on-premise WCF services to Azure is a strategic move for enterprises seeking increased flexibility, scalability, and cost-efficiency.

Top Articles
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 5712

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.