WCF tutorial | WCF IIS Hosting
IIS 5/6 Hosting
The main advantage of hosting service in IIS is that, it will automatically launch the host process when it gets the first client request. It uses the features of IIS such as process recycling, idle shutdown, process health monitoring and message based activation. The main disadvantage of using IIS is that, it will support only HTTP protocol.
Let as do some hands on, to create service and host in IIS
Step 1:Start the Visual Studio 2008 and click File->New->Web Site. Select the 'WCF Service' and Location as http. This will directly host the service in IIS and click OK.
I have created sample HelloWorld service, which will accept name as input and return with 'Hello' and name. Interface and implementation of the Service is shown below.
public interface IMyService
string HelloWorld(string name);
public class MyService : IMyService
#region IMyService Members
public string HelloWorld(string name)
return "Hello " + name;
Service file (.svc) contains name of the service and code behind file name. This file is used to know about the service.
<%@ ServiceHost Language="C#" Debug="true"
Service="MyService" CodeBehind="~/App_Code/MyService.cs" %>
Server side configurations are mentioned in the config file. Here I have mention only one end point which is configured to 'wsHttpBinding', we can also have multiple end point with differnet binding. Since we are going to hosted in IIS. We have to use only http binding. We will come to know more on endpoints and its configuration in later tutorial.
<service behaviorConfiguration="ServiceBehavior" name="MyService">
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<!-- To avoid disclosing metadata information,
set the value below to false and remove the
metadata endpoint above before deployment -->
<!-- To receive exception details in faults for
debugging purposes, set the value below to true.
Set to false before deployment to avoid disclosing exception information -->
You need to mention the service file name, along with the Address mention in the config file.
IIS Screen shot
This screen will appear when we run the application.
Now we successfully hosted the service in IIS. Next we have to consume this service in client application. Before creating the client application, we need to create the proxy for the service. This proxy is used by the client application, to interact with service.
To create the proxy, run the Visual Studio 2008 command prompt. Using service utility we can create the proxy class and its configuration information.
After executing this command we will find two file generated in the default location.
Now we will start creating the Console application using Visual Studio 2008(Client application).
Add the reference 'System.ServiceModel'; this is the core dll for WCF.
Create the object for the proxy class and call the HelloWorld method.
static void Main(string args)
//Creating Proxy for the MyService
MyServiceClient client = new MyServiceClient();
Console.WriteLine("Client calling the service...");
If we run the application we will find the output as shown below.
I hope you have enjoyed the Service hosted in IIS. Now let start the look on the self hosted service.
Comments on this article
- Always create the service with Interface->Implementation format, mention the contract in Interface.
- Define the service in Class library and refer the class library in Host project. Donâ€™t use service class in host project.
- Change the instance mode to per call as default.
- Always catch exception using try/catch block and throw exception using FaultException < T >.
- Logging and Include exception should be enable while compiling the project in debug mode. While in production deployment disable the logging and Include exception details.