Silverlight, WPF, ASP.NET

WCF: This collection already contains an address with scheme http

Wednesday, January 24, 2007 11:30:30 PM (Eastern Standard Time, UTC-05:00)

The Problem:

I recently ran into this problem issue trying to host a service through my shared web hosting service provider.

Server Error in '/' Application.

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

The Solution:

The solution, since WCF services hosted in IIS can have only one Base Address was to create a custom service factory to intercept and remove the additional unwanted base addresses that IIS was providing. In my case IIS was providing:

domain.com
www.domain.com
dedicatedserver1234.hostingcompany.com

We are able to customize our .svc file to specif a custom serive factory

<%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService"
Factory="Microsoft.ServiceModel.Samples.CustomHostFactory" %>

We are then able to create our custom factory by inheriting from ServiceHostFactory and overriding as required

    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();
        }
    }

In my case I decided to pass through baseAddresses[1] (which was the www. address) but it would probably be wise to specify that address you want to prevent changes by your web host from impacting yoru code.

Big thanks to those at WebHost4Life that put the time into helping me resolve this.

 

Technorati tags:

 

 

Posted in  | Comments [13]