Tag Archive | "wcf service"

Tags:

This collection already contains an address with…


How to solve "This collection already contains an address with scheme http. There can be at most one address per scheme in this collection" error?

You may encounter following error when you are running WCF related script on server:

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

It is because IIS setting, by default, each IIS entry will hold more then one hostheader. (i.e. more then one address) It can be solved by customize your .svc file to specific a custom ServiceHostFactory.

Let’s say your entry have two hostname listed:
domain.com
www.domain.com

If you like to use "www.domain.com" as ServiceHostFactory. You can now inherit from ServiceHostFactory and override it like follow sample:

    class CustomHostFactory : ServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
              CustomHost customServiceHost =
                new CustomHost(serviceType, baseAddresses[1]);
            return customServiceHost;
        }
    }
    class CustomHost : ServiceHost
    {
        public CustomHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        { }
        protected override void ApplyConfiguration()
        {
            base.ApplyConfiguration();
        }
    }

If you like to use "domain.com" as ServiceHostFactory, you should replace  baseAddresses[1] with baseAddresses[0].

Share

Posted in Windows-IIS-ASPNETComments (5)