How to Resolve WCF Issue: Can’t host WCF service in a website with multiple identities

When a WCF service is hosted in a IIS website which has multiple identities, that is, responds on different hostnames/ports, the WCF service, when created, throws the exception below:

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.
Parameter name: item

I am aware that this behavior is by design and I believe that it can be resolved. I’m searching to forums, MSDN and blogs but no solution was found. I’m only found one solution to create a custom ServiceHostFactory which filters the additional base addresses and instantiates the service on one of them only. Thank to Zeddy for the helps. This issue can be resolved by creating a custom ServiceHostFactory which clear all baseAddresses then override Behaviors and ServiceEndPoint described below.

Create Custom ServiceHostFactory

Create new class for custom ServiceHostFactory below.

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Linq;
using System.ServiceModel.Description;

public class MultipleIISBindingSupportServiceHostFactory : ServiceHostFactory
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        // return the emply list Uri to make it automatically select baseAddresses by endpoint configuration
        var host = base.CreateServiceHost(serviceType, new Uri[] {/*empty*/ });

        // Setup MEX dynamically
        var behavior = new ServiceMetadataBehavior
                               HttpGetEnabled = true,
                               HttpGetUrl = baseAddresses.Where(addr => addr.Scheme == "http").First()

        // Setup Endpoint configuration dynamically
        foreach (var uri in baseAddresses)
            // Service endpoint support http scheme only, exclude https scheme
            if (uri.Scheme == "http")
                                        new BasicHttpBinding(BasicHttpSecurityMode.None),
        return host;

Modify WCF Service Markup

To modify WCF Service Markup, right click on the MyService.svc file and then click “View Markup”.

<%@ ServiceHost Language="C#" Debug="true" Service="MyService" CodeBehind="MyService.svc.cs" Factory="MultipleIISBindingSupportServiceHostFactory" %>

Modify Web.config File

Open web.config file and going to line with <system.serviceModel> element, replace <system.serviceModel> and all child elements with following config.

		<messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true"/>
	<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

After all steps above was done, the multiple identities issue should be resolved.

I hope this tips will be helpful.