Testing services on your local machine is grand, though there is often a hiccup once you put it on your live server. Such was the case when I tried hosting a simple service on my website to test the server's configuration. I received the following error:
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 currently hosting my website with a shared hosting provider, webhost4life. Luckily Rob Zelt had already encountered this problem with the same hosting provider.
The error stems from the website being configured to use both:
xamlcoder.com
www.xamlcoder.com
You can find this configuration by opening up the IIS Manager snap-in and right-clicking on the "Default Web Site". Select Properties and on the "Web Site" tab in the "Web Site Identification" click the "Advanced" button. That should bring up the "Advanced Multiple Web Site Configuration" dialog box where these are listed.
Because I want to use both, the solution is to provide your own ServiceHostFactory. Rob Zelt has his own, and later points to points to Aaron Skonnard's solution.
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
class CustomHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
if (baseAddresses.Length > 1)
{
return new CustomHost(serviceType, baseAddresses[1]);
}
else
{
return base.CreateServiceHost(serviceType, baseAddresses);
}
}
}
class CustomHost : ServiceHost
{
public CustomHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{ }
protected override void ApplyConfiguration()
{
base.ApplyConfiguration();
}
}
Once you have the class defined, you can point your service to it in the .svc file.
<% @ServiceHost Language=C#
Debug="true"
Service="MyService"
Factory="CustomHostFactory"
CodeBehind="~/App_Code/Service.cs" %>
A bit of extra work though a fairly simple solution!
