13 September, 2009

WCF : This collection already contains an address with scheme http.

Trying to host more than one WCF-services on a remote hosted IIS; I today encountered this problem when trying to open the second service:

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

WCF services hosted in IIS can have only one baseaddress. So to mitigate that problem, you have to create a custom service factory to intercept and remove the additional unwanted base addresses that IIS is providing. A generic factory capable of handling any service type is seen here:

namespace Services.CKIT.dk
{
public class GenericServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
//return the first...
return new ServiceHost(serviceType, baseAddresses[0]);
}
}
}



You configure it in your service configuration files (myservice.svc) by means of the Factory attribute on the ServiceHost tag:



<%@ ServiceHost 
Service="Services.CKIT.dk.Phone.BackupService"
Factory="Services.CKIT.dk.GenericServiceHostFactory"
%>
<%
@ Assembly Name="services.ckit.dk" %>


In this way – you handle the service construction yourself and do not leave it to the WCF-pipeline.

No comments:

iPhone/XCode - not all cases are equal!

This bit me! Having made some changes to an iPhone application (Obj-C); everything worked fine in the simulator. But, when deploying the s...