If you ever tried hosting a WCF Service on an IIS site with Multiple Bindings you will be familiar with the error "This collection already contains an address with scheme http...". The workarounds you can find on the internet and the recent introduction of baseAddressPrefixFilters in WCF can help get passed this error, but they won't allow you to receive requests on all the configured bindings which is usually what people I talked to actually want to do. So here's how you would go by making your service accessible from all available bindings:
1) Use a custom Factory, here's what this looks like in your .svc file:
<%@ ServiceHost Language="C#" Debug="true" Service="MyService.MyService" Factory="MyService.MultipleHostsFactory" %>
2) The implementation, rather than selecting a single address from the list that is passed in, overrides this with a completely empty list:
class MultipleHostsFactory : ServiceHostFactory { protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { return base.CreateServiceHost(serviceType, new Uri[] { }); } }
3) Your web.config will now need to specify the full list of absolute addresses you're effectively listening to. This needs to exactly match your IIS configuration or this won't work:
<configuration> <system.serviceModel> <services> <service name="MyService.MyService"> <endpoint address="http://hiring.contoso.com/WcfMultipleHosts/test.svc" binding="basicHttpBinding" contract="MyService.IServiceContract" /> <endpoint address="http://hiring.contoso.com.uk/WcfMultipleHosts/test.svc" binding="basicHttpBinding" contract="MyService.IServiceContract" /> </service> </services> </system.serviceModel> </configuration>
Done.
Be aware that anything that relies on base addresses will now also need to be converted to explicit full addresses, here's an example of how you'd enable the help page for httpGet and enable service metadata:
<configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="diagnose"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://hiring.contoso.com/WcfMultipleHosts/test.svc" httpsGetEnabled="false" /> <serviceDebug httpHelpPageUrl="http://hiring.contoso.com.uk/WcfMultipleHosts/test.svc" httpsHelpPageEnabled="false" includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="diagnose" name="MyService.MyService"> <endpoint address="http://hiring.contoso.com/WcfMultipleHosts/test.svc" binding="basicHttpBinding" contract="MyService.IServiceContract" /> <endpoint address="http://hiring.contoso.com.uk/WcfMultipleHosts/test.svc" binding="basicHttpBinding" contract="MyService.IServiceContract" /> </service> </services> </system.serviceModel> </configuration>