Problems hosting WCF on a web server with multiple host headers

This ended up being a bit of a kick-in-the-pants this week, so I definitely wanted to summarize and write this down – as it’s bound to come up again!
You’re minding your business, not bothering anybody and you want to have your ASP.NET hosted WCF and host it out on an IIS server. The service worked great while being hosted in Visual Studio. But when you push to an IIS server, you get an error saying that there are too many baseAddresses registered.
IIS can handle and route many different websites at a time. It does this by look for the "host header" in the request. In other words, it’s looking to see what the server name was, in what you typed in the URL. If it finds a match, if shows you that specific website.Well, in the case of a shared IIS environment (like in many companies – or at an Internet web host), there may be multiple real names for the server you are on. The problem comes in when WCF queries IIS to go get a default "base address" and IIS returns several.
I blew about a day on this and pieced together some info from a couple of blogs and from MSDN. In short, here’s the deal: you basically have to have your service initialize using a custom/factory ServiceHost. When you do this, you can control precisely how those base addresses are handle. So first, create a couple of classes (these can just go in App_Code, if you wish:

using System;

using System.ServiceModel;

using System.ServiceModel.Activation;


public class CustomHostFactory : ServiceHostFactory


    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)


        CustomHost customServiceHost =

            new CustomHost(serviceType, baseAddresses[0]);

        return customServiceHost;




public class CustomHost : ServiceHost


    public CustomHost(Type serviceType, params Uri[] baseAddresses)

        : base(serviceType, baseAddresses)

    { }


    protected override void ApplyConfiguration()





Next, in the .svc file (the WCF equivalent of an .asmx), you would have something like this:

<%@ ServiceHost Language="C#" Debug="true"


    Factory="CustomHostFactory" %>

Now, in the case above – I just said "take whatever the first base address is" – but you may want/need to apply more logic than that. Anyhow, there it is…
Posted in .NET 3.5, Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

%d bloggers like this: