Click here to Skip to main content
Click here to Skip to main content

A custom ServiceHostFactory

By , 8 Sep 2008
 

Introduction

Well, this is my first article here, and I'll try to be short. Sorry about my English, it isn't very good.

There are two main ways to host a WCF Service. One approach is to write a server, typically as a console app. The other is to use a managed hosting environment (e.g., Internet Information Services (IIS) or Windows Process Activation Service (WAS)).

When using the second approach, the creation of the services hosts is done automatically, based on a configuration file (Web.config), where the endpoints are defined. But, there are situations when the service hosts must be created programmatically.

In this article, I'll explain how to customize the host creation when hosting the service in a managed hosting environment.

Background

I spent some time searching for a way to create endpoints without using a Web.config file when hosting a service using IIS. And, all that I found was a small example (two lines!) in MSDN. So, here I will show a more complete solution.

First, we need to familiarize with some classes.

Framework 3.5

  • WebServiceHostFactory: "A factory that provides instances of WebServiceHost in managed hosting environments where the host instance is created dynamically in response to incoming messages". In other words: IIS and WAS use this class to create Service Hosts.
  • WebServiceHost: Instances of this class are created by WebServiceHostFactory.

Using the code

First, we need to write a class that derives from WebServiceHostFactory. Let's call it MyWebServiceHostFactory. In this class, all we need to do is to override the CreateServiceHost method.

In this method, we create the host the way we want.

protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
    ServiceHost host = new ServiceHost(serviceType, baseAddresses);

    foreach (Uri address in baseAddresses)
    {
        WSHttpBinding b = new WSHttpBinding(SecurityMode.Message);

        b.Name = serviceType.Name;

        host.AddServiceEndpoint(serviceType.GetInterfaces()[0], b, address);
    }
    return host;
}

Now, its time to tell the host environment to use our HostFactory. It's very simple. We just need to set the property Factory in the .svc file, as shown below:

<% @ ServiceHost Language="C#" Service="WcfService1.Service1" 
                 Factory="WcfService1.MyWebServiceHostFactory" %>

Well done! Now, the host environment will call the MyWebServiceHostFactory.CreateServiceHost method to create the service hosts.

Points of interest

This approach allows you to host a service on IIS or WAS without configuring endpoints and bindings in the Web.config. Because, you can create it programmatically with your custom ServiceHostFactory. Now, for example, one could load the endpoints information from a database, or from a service.

Next step

The next step is to provide ASP.NET AJAX support to the Service Hosts created. To do so, I will write a factory that derives from WebScriptServiceHostFactory.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

cesar_boucas
Web Developer
Brazil Brazil
Member
Yesterday C++, today C#.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberEvil_jamg4 Jan '13 - 5:18 
Very Helpful Thank you!!
GeneralExtending Hosting Using ServiceHostFactory ( article by MSDN )membercesar_boucas12 Jul '10 - 8:38 
http://msdn.microsoft.com/en-us/library/aa702697.aspx[^]
"Go for it!"

GeneralMuito obrigadomemberRoni Peterson27 Nov '09 - 9:16 
Cesar, ótimo exemplo de como se utiliza esta classe pra resolver este problema. Pra quem está começando com WCF muitas coisas não são assim tão simples, tenha certeza de que este exemplo me ajudou muito.
 
[]'s
GeneralRe: Muito obrigadomembercesar_boucas28 Nov '09 - 1:00 
Roni, fico feliz que tenha lhe ajudado. Muito obrigado.
 
"Go for it!"

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 8 Sep 2008
Article Copyright 2008 by cesar_boucas
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid