The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  87 Posts :: 0 Stories :: 56 Comments :: 2 Trackbacks

Ads

 

Donate via PayPal...

...if you feel the site helped.

Archives

Post Categories

Open Source Projects

Other Blogs

Hosting WCF services in IIS is a breeze, however, an interesting error along the lines of:

'This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection. Parameter name: item '

According to a response on one of the MSDN forums, "WCF does not support multiple IIS bindings for the same protocol (here it is HTTP) for the same web site". Included in this article is code for creating you own custom service host factory that allows you to get around this.

Download

CustomServiceHostFactory.zip (UPDATE 25/03/2008: As pointed out below by Johan, the custom service host class in the example wasn't needed, just the factory. I've removed it from the example.)

The Code

 The code is relatively simple, with the crux of the work being done in the overriden method CreateServiceHost. What would normally happen is that ServiceHosts would be create for all the baseAddresses passed in, which is where WCF comes to a grinding halt.  

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        CustomServiceHost customServiceHost;
        if (baseAddresses.Length > 1)
            customServiceHost = new CustomServiceHost(serviceType, baseAddresses[1]);
        else
            customServiceHost = new CustomServiceHost(serviceType, baseAddresses[0]);
        return customServiceHost;
    }

By checking whether or not there are multiple address, and only choosing one of them, the process continues seemlessly. The code included  merely demonstrates how to go about creating your own factory - a good enhancement would be to allow the coder to specificy which address, either by index or address, he would actually like to choose.

Once you have your factory included in your WCF service project (the project with the .svc files), its important to modify the svc files to include the factory information in the ServiceHost tag: 

<%@ServiceHost Factory="CustomServiceHostFactory" language=c# Debug="true" Service="MyService, MyServiceAssembly"  %>

Now you can continue using IIS as your hosting platform for WCF.

posted on Thursday, February 21, 2008 6:40 PM

Feedback

# re: Custom ServiceHostFactory for WCF and IIS 3/20/2008 10:06 PM Johan Stokking
There is no need for a custom service host class for this purpose. The CustomServiceHost is useless as it does not add any functionality. Just create a ServiceHost in the custom factory and you're done.

# re: Custom ServiceHostFactory for WCF and IIS 3/25/2008 8:02 AM Darksider
Thanks, you're so right - these things happen when you pull the code out of a project and strip out the unnecessary.
I'll update the sample code now.

Comments have been closed on this topic.