Blog

WCF and Multiple IIS Site Bindings

December 1, 2008

We ran into an issue last week when we were deploying a WCF service on an IIS web site which had multiple IIS bindings. It manifested itself by throwing the following exception:

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.

Turns out that multiple IIS site bindings results in multiple base addresses while WCF (.NET 3.0) only supports a single base address in this scenario. Fortunately there are a couple of solutions to bypass this annoying shortcoming. First, you can provide a filter for the base addresses by providing a custom ServiceHostFactory:

public class InvoiceServiceHostFactory 
    : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost
        (Type serviceType, Uri[] baseAddresses)
    {
        Uri preferredHostBaseAddress = 
            Settings.PreferredHostBaseAddress;
        return base.CreateServiceHost(serviceType, 
                                      preferredHostBaseAddress);
    }    
}

This custom ServiceHostFactory can now be used by specifying it in the .svc file like so:

<%@ ServiceHost Language=”C#” 
                Debug=”true” 
                Service=”InvoiceService.InvoiceService” 
                Factory=”InvoiceService.Wcf.InvoiceServiceHostFactory“
                CodeBehind=”InvoiceService.svc.cs” %%>

This has the major disadvantage that requests can only be received by a single base address, which we retrieve from the configuration file in this case. To solve this issue, we can tweak our custom ServiceHostFactory like so:

public class InvoiceServiceHostFactory 
    : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost
        (Type serviceType, Uri[] baseAddresses)
    {
        return base.CreateServiceHost(serviceType, 
                                      new Uri[0]);
    }    
}

Now we have to provide a base address for each WCF binding in the configuration file:

<service name="InvoiceService.InvoiceService">
    <endpoint 
        address="http://huppeldepup.be/InvoiceService/InvoiceService.svc" 
        binding="wsHttpBinding" 
        contract="InvoiceService.IInvoiceService" />
    <endpoint 
        address="http://huppeldepup.com/InvoiceService/InvoiceService.svc" 
        binding="wsHttpBinding" 
        contract="InvoiceService.IInvoiceService" />
</service>

This way, we can start receiving requests through multiple bindings, which is what we wanted in the first place. This approach has one disadvantage though. When a corresponding IIS binding ever changes, we also have to remember to check every configuration of every WCF service that lives inside the respective IIS web site. This is error prone and involves friction.

If you are using .NET 3.5 however, then this is your lucky day. WCF that comes with .NET 3.5 now supports BaseAdressPrefixFilters.

<serviceHostingEnvironment>
    <baseAddressPrefixFilters>
    <add prefix="http://huppeldepup.be/"/>
        <add prefix="http://huppeldepup.com"/>
    </baseAddressPrefixFilters>
</serviceHostingEnvironment>

There you go. Too bad this feature is not available with .NET 3.0. Until you switch to .NET 3.5, you have to roll your own solution I’m afraid.

Till next time.

Profile picture of Jan Van Ryswyck

Jan Van Ryswyck

Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Comments

About

Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Contact information

(+32) 496 38 00 82

infonull@nullprincipal-itnull.be