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:
public class CustomHostFactory : ServiceHostFactory
protected override ServiceHost CreateServiceHost(Type serviceType, Uri baseAddresses)
CustomHost customServiceHost =
new CustomHost(serviceType, baseAddresses);
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"
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…