Saturday, March 29, 2008

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

Description:
When you try to access a WCF service hosted in IIS, you get the following error:

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

Context:
You have a website hosted in IIS and the site has multiple host headers defined for the site (e.g. www.domain.com, domain.com).

Fix:
There doesn’t appear to be a nice workable solution for this as of VS 2008 initial release on .NET 3.5. The only “hack” found out there is to define a new Factory for the service.

Step 1:

namespace Foo
{
public class CustomHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(
Type serviceType,
Uri[] baseAddresses)
{
// Specify the exact URL of your web service from the config file:
// e.g. http://www.domain.com/service/myservice.svc
Uri webServiceAddress =
new Uri(ConfigurationManager.AppSettings["ServiceUri"]);

ServiceHost webServiceHost =
new ServiceHost(serviceType, webServiceAddress);

return webServiceHost;
}
}
}

Some web articles had you also creating a new host derived from ServiceHost, but that is not needed.

Step 2:
Modify the .svc file on the site hosting the service (not the .svc in Visual Studio):

<%@ ServiceHost Service="Foo.Service1" Factory="Foo.CustomHostFactory" %>

The above .svc content may also include the language and debug specifiers.

No comments:

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...