Silverlight SVC Web Service problems on IIS

Since WCF services hosted in IIS can have only one Base Address so we can create a custom service factory to intercept and create a System.ServiceModel.ServiceHost for a specific type of service with a dynamic URI or base address that is from the request URL.

The Error.

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
Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[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]
   System.ServiceModel.UriSchemeKeyedCollection.InsertItem(Int32 index, Uri item) +11520590
   System.Collections.Generic.SynchronizedCollection`1.Add(T item) +67
   System.ServiceModel.UriSchemeKeyedCollection..ctor(Uri[] addresses) +49
   System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) +129
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses) +28
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +331
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath) +11659932
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +42
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +479

[ServiceActivationException: The service '/SilverlightMapService.svc' cannot be activated due to an exception during compilation.  The exception message is: This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection. 
Parameter name: item.]
   System.ServiceModel.AsyncResult.End(IAsyncResult result) +11531006
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +194
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, Boolean flowContext) +176
   System.ServiceModel.Activation.HttpHandler.ProcessRequest(HttpContext context) +23
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75


Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082

 

The Solution,

Add this class to your solution

public class CustomHostFactory : ServiceHostFactory 
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
              CustomHost customServiceHost =
                new CustomHost(serviceType, baseAddresses[0]);
            return customServiceHost;
        }
    }

 public   class CustomHost : ServiceHost
    {
        public CustomHost(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        { }
        protected override void ApplyConfiguration()
        {
            base.ApplyConfiguration();
        }
    }
 
On the web service add:
<%@  ServiceHost Language="C#" Debug="true" Factory="mynamespace.CustomHostFactory" Service="mynamespace.SilverlightService" CodeBehind="SilverlightService.svc.cs" 

%>

Hope this helps

Cheers

Al

#1 b a r s » Blog Archive » Silverlight SVC Web Service problems on IIS : Al Pascual on 4.09.2009 at 12:41 PM

Pingback from b a r s » Blog Archive » Silverlight SVC Web Service problems on IIS : Al Pascual

#2 Matt Watson on 4.11.2009 at 7:03 AM

Also look at this

geekswithblogs.net/.../129655.aspx