Click here to Skip to main content
15,888,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My web application already hosting a couple of WCF services with their own custom ServiceHost. Now I want to add webhttpbinding to host my REST service but whenever I access the service it throw this exception:
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. If your service is being hosted in IIS you can fix the problem by setting 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' to true or specifying 'system.serviceModel/serviceHostingEnvironment/baseAddressPrefixFilters'


Enable the "multipleSiteBindingsEnabled" causing the existing WCF service to throw this exception:
The exception message is: When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true in configuration, the endpoints are required to specify a relative address


My existing WCF service http://localhost:8080/Services/Service1.svc has multiple bindings depending on the binding set in IIS, ie nettcp (NetTcpBinding), https (CustomBinding) and http (wsHttpBinding)

For my new REST (different contract) service I create a separate host and I am unable to access it due to the first exception. What I need to do in order to make this work? I do not put my configurations in web.config because I need to be able to create ServiceHost in code.

REST Service host code:

C#
public class RestFactory : WebServiceHostFactory
  {
    protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
      WebServiceHost host = (WebServiceHost)base.CreateServiceHost(serviceType, baseAddresses);
      
      ServiceEndpoint endpoint = host.AddServiceEndpoint(serviceType, new WebHttpBinding(), "rest");
      
      endpoint.Behaviors.Add(new WebHttpBehavior { AutomaticFormatSelectionEnabled = false, HelpEnabled = true });
      
      return host;  // base.CreateServiceHost(serviceType, baseAddresses);
    }
  }



Existing WCF host code:

C#
public class WCFAgent:ServiceHostFactory
{
  protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
  {
    ServiceHost host;

    if (baseAddresses.Length > 1)
    {
      host = new ServiceHost(serviceType, baseAddresses[1]);
    }
    else
    {
      host = new ServiceHost(serviceType, baseAddresses[0]);
    }
    host.CloseTimeout = TimeSpan.FromMinutes(3);
    host.OpenTimeout = TimeSpan.FromMinutes(3);

    foreach (Uri address in baseAddresses)
    {
      if (address.Scheme == "https")
      {
        CustomBinding customBinding = new CustomBinding();
        TextMessageEncodingBindingElement textMsgEncoding = new TextMessageEncodingBindingElement();
        System.Xml.XmlDictionaryReaderQuotas rdrQuotas = new System.Xml.XmlDictionaryReaderQuotas();
        rdrQuotas.MaxDepth = 64;
        rdrQuotas.MaxStringContentLength = 25600000;// 2147483647;
        rdrQuotas.MaxArrayLength = 25600000;//2147483647;
        rdrQuotas.MaxBytesPerRead = 4096;
        rdrQuotas.MaxNameTableCharCount = 16384;
        textMsgEncoding.ReaderQuotas = rdrQuotas;
        customBinding.Elements.Add(textMsgEncoding);
        customBinding.CloseTimeout = new TimeSpan(0, 3, 0);
        customBinding.OpenTimeout = TimeSpan.FromMinutes(3);
        customBinding.ReceiveTimeout = TimeSpan.FromMinutes(3);
        customBinding.SendTimeout = TimeSpan.FromMinutes(3);
        customBinding.Elements.Add(new ReliableSessionBindingElement());

        HttpsTransportBindingElement httpsTransportBindElement = new HttpsTransportBindingElement();
        httpsTransportBindElement.AllowCookies = false;
        httpsTransportBindElement.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
        httpsTransportBindElement.MaxReceivedMessageSize = 25600000;
        customBinding.Elements.Add(httpsTransportBindElement);
        host.AddServiceEndpoint("WebClient.WebServices.IWCFWork", customBinding, address);
      }
      else if (address.Scheme=="net.tcp")
      {
        NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None, true);
        tcpBinding.Name = serviceType.Name;
        tcpBinding.MaxReceivedMessageSize = 2147483647;
        tcpBinding.CloseTimeout = TimeSpan.FromMinutes(3);
        tcpBinding.SendTimeout = TimeSpan.FromMinutes(3);
        tcpBinding.OpenTimeout = TimeSpan.FromMinutes(3);
        tcpBinding.ReceiveTimeout = TimeSpan.FromMinutes(3);

        System.Xml.XmlDictionaryReaderQuotas rdrQuotas = new System.Xml.XmlDictionaryReaderQuotas();
        rdrQuotas.MaxDepth = 64;
        rdrQuotas.MaxStringContentLength = 25600000;// 2147483647;
        rdrQuotas.MaxArrayLength = 25600000;//2147483647;
        rdrQuotas.MaxBytesPerRead = 4096;
        rdrQuotas.MaxNameTableCharCount = 16384;
        tcpBinding.ReaderQuotas = rdrQuotas;
        host.AddServiceEndpoint("WebClient.WebServices.IWCFWork", tcpBinding, address);
      }
      else
      {
        WSHttpBinding binding = new WSHttpBinding(SecurityMode.None, true);
        binding.Name = serviceType.Name;
        binding.AllowCookies = false;
        binding.CloseTimeout = TimeSpan.FromMinutes(3);
        binding.SendTimeout = TimeSpan.FromMinutes(3);
        binding.OpenTimeout = TimeSpan.FromMinutes(3);
        binding.ReceiveTimeout = TimeSpan.FromMinutes(3);
        binding.MaxReceivedMessageSize = 51200000; //
        System.Xml.XmlDictionaryReaderQuotas rdrQuotas = new System.Xml.XmlDictionaryReaderQuotas();
        rdrQuotas.MaxDepth = 64;
        rdrQuotas.MaxStringContentLength = 51200000;
        rdrQuotas.MaxArrayLength = 51200000;
        rdrQuotas.MaxBytesPerRead = 4096;
        rdrQuotas.MaxNameTableCharCount = 16384;
        binding.ReaderQuotas = rdrQuotas;
        host.AddServiceEndpoint("WebClient.WebServices.IWCFWork", binding, address);
      }
    }
    //return base.CreateServiceHost(serviceType, baseAddresses);
    return host;
  }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900