WCF: This collection already contains an address with scheme http

Wednesday, January 24, 2007 8:30:30 PM (GMT Standard Time, UTC+00: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 wcf  | Comments [13] 


Tuesday, April 03, 2007 7:29:14 PM (GMT Daylight Time, UTC+01:00)
Hi, I am having the exact same problem,
this solution should be included as a httpHandler ?

I'm not sure on exactly how to implement it.

regards,
Alexandre Brisebois
Tuesday, April 03, 2007 7:46:49 PM (GMT Daylight Time, UTC+01:00)
In your .svc file, you need to specify the custom factory:

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

The custom class code listed in the post then needs to be included in your App_Code folder. Basically this tells WCF to create the service using your factory, not the standard.
Rob Zelt
Friday, November 09, 2007 4:59:40 PM (GMT Standard Time, UTC+00:00)
How do I handle this when having two separate bindings requring 2 separate protocols: I have https for my service but http for the MexdataExchange. I set the factory to use the https base address, but then MexdataExchagne doesn't work.

Thanks for any info...
Graeme Hood
Wednesday, December 12, 2007 6:51:49 PM (GMT Standard Time, UTC+00:00)
Thanks for the easy heads-up. I ran into this problem and was caught a little off guard. All fixed up now!

Regards, Michael
Tuesday, January 01, 2008 6:39:43 PM (GMT Standard Time, UTC+00:00)
Thank you for this! We gave you credit in out CodePlex project:

www.codeplex.com/IWeb/SourceControl/FileView.aspx?itemId=90640&changeSetId=5438
Wednesday, January 30, 2008 3:15:59 AM (GMT Standard Time, UTC+00:00)
I've been a little more used to Apache servers in the past but recently got involved with an IIS setup.

Came across this article in Google whilst searching for a solution to the problem, you're a godsend rob.

Best Regards
Stuart

Wednesday, March 05, 2008 4:24:02 AM (GMT Standard Time, UTC+00:00)
If you have .NET 3.5 installed at your hosting provider, there is now an even easier way:

http://blogs.msdn.com/rampo/archive/2008/02/11/how-can-wcf-support-multiple-iis-binding-specified-per-site.aspx
Monday, March 10, 2008 2:20:56 AM (GMT Standard Time, UTC+00:00)
Thank you! I can stop pulling my hair out now.
Scott Klueppel
Monday, March 17, 2008 1:44:06 PM (GMT Standard Time, UTC+00:00)
Hey, same hosting provider, same problem, same solution worked. Thanks!
Thursday, April 17, 2008 5:49:20 AM (GMT Daylight Time, UTC+01:00)
We have the same hosting provider, same problem, but it
gave me another problem, it now prompts for a user name and password. Any ideas?

Thursday, April 17, 2008 10:14:43 PM (GMT Daylight Time, UTC+01:00)
Suppose I want to access my service via www.domain.com AND domain.com?
Rick
Wednesday, July 30, 2008 12:41:57 AM (GMT Daylight Time, UTC+01:00)
if you want to listen to all bindings, here's how to accomplish that:
http://ranamauro.blogspot.com/2008/07/hosting-wcf-service-on-iis-site-with_25.html
Saturday, October 04, 2008 7:31:52 PM (GMT Daylight Time, UTC+01:00)
I'm trying to do the same thing with DataServiceHostFactory to work with a ADO.NET Data Service:

[code]
class CustomDataServiceHostFactory : DataServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new ServiceHost(serviceType, baseAddresses[0]);
}
}
[/code]

But then I got an error that there were no endpoints...they're automatically configured by ADO.NET Data Service.

So I put the following in App.Config

[code]
<service name="AlpineDesert.SilverthorneWeb.DataService.DataModel"
behaviorConfiguration="DataServiceTypeBehaviors">
<endpoint contract="System.Data.Services.IDataServiceHost"
binding="basicHttpBinding" bindingConfiguration="userHttp"/>
</service>
[/code]

But my blind guess on endpoint contract was wrong, so I got "The contract name 'System.Data.Services.IDataServiceHost' could not be found in the list of contracts implemented by the service 'DataModel'."

Any ideas?
Wes Cooper
Comments are closed.