Ads by The Lounge
I recently ran into this problem issue trying to host a service through my shared web hosting service provider.
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, 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.comwww.domain.comdedicatedserver1234.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.