WCF service hosted in IIS - "This collection already contains an address with scheme http" - Why does this happen?

I came across this error when I tried hosting a WCF endpoint from a website in IIS. This can be resolved by adding the following section in the web.config file

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">


        <add prefix=http://www.myhost.com/ />




Why does this happen?

When you host a website using a shared hosting provider, it is possible that there are multiple IIS bindings for the site. This results in multiple base addresses for each scheme which WCF doesn't support. Hence, we use the baseAddressPrefixFilters to filter out the IIS bindings and only use the relevant one for the WCF endpoint.

To get a better and complete picture read through the Remarks section over here: http://msdn.microsoft.com/en-us/library/bb924481.aspx