Hosting WCF In IIS7 With Multiple Website Bindings

In IIS7 (should be similar in IIS6 as well) Manager, you can create a website under Sites with multiple Bindings so that the website root can be accessed by different combinations of host name, IP and port etc. However, if you have a WCF service that you intend to host in this website, there are some explicit configuration that must follow.

Firstly, understand that a Website in ISS is an Application that runs in an Application Pool. As such, it is allowed to have a folder named “bin” or a bin folder that will store dll binaries that your WCF configuration .svc and web.config file will load. The .svc and web.config file can reside in any folder or subfolders in the Website Application, but it will always load the binaries from the Application’s bin folder.

When multiple Bindings are set on the Website, you will need additional settings to web.config to explicitly state the bindings that the WCF service can be reached.

<system.serviceModel>
    <serviceHostingEnvironment>
        <baseAddressPrefixFilters>
            <add prefix="net.tcp://first.host.name:80"/>
            <add prefix="http://another.host.name:9000"/>
        </baseAddressPrefixFilters>
    </serviceHostingEnvironment>
</system.serviceModel>

Or…

<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
    </serviceHostingEnvironment>
</system.serviceModel>

However, this then requires that the WCF service be hosted in an Application domain separate from Website Application. One solution is to put the WCF web.config, .svc and the bin folder into a subfolder, and make the subfolder an Application itself. Because the subfolder is now an Application (within the Website Application), it will have it will load the dll from its own bin folder.