WCF and Multiple IIS Site Bindings

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.

2 thoughts on “WCF and Multiple IIS Site Bindings

  1. I have the site which has two http headers one for localhost and other is the live domain name (www.ss.com)

    I want the service to be avaliable at both ends.

    http://localhost/VitualDirectory/service.svc
    http://ss.com/service.svc

    But I am gettting error on using the method you have decribed
    Could not find a base address that matches scheme http for the endpoint with binding MetadataExchangeHttpBinding. Registered base address schemes are [].

    at
    return base.CreateServiceHost(serviceType, new Uri[0]);

    Please suggest

Comments are closed.