1

I have read some posts here, claiming that WCF can allow http and https on the same endpoint/port, but that sounds a bit wrong to me. I tried to set it up but I cannot figure out how to enable both https and http listening on the same port/ip, programmtically.

I get the following error when I try to add https:

'System.ServiceModel.AddressAlreadyInUseException' in System.ServiceModel.dll HTTP could not register URL https://+:10901/Service/. Another application has already registered this URL with HTTP.SYS.

It works fine if I add just one of them.

Code:

Uri httpsUri = new Uri("https://" + localIp.ToString() + ":" + settings._WebservicePort.ToString() + "/Service/");
Uri httpUri = new Uri("http://" + localIp.ToString() + ":" + settings._WebservicePort.ToString() + "/Service/");

host = new WebServiceHost(typeof(Service), httpUri, httpsUri);

WebHttpBinding whbHttp = new WebHttpBinding
{
    CrossDomainScriptAccessEnabled = true,
    Security = { Mode = WebHttpSecurityMode.None },
    MaxReceivedMessageSize = 10000000
};

WebHttpBinding whbHttps = new WebHttpBinding
{
    CrossDomainScriptAccessEnabled = true,
    Security = { Mode = WebHttpSecurityMode.Transport },
    MaxReceivedMessageSize = 10000000
};
ServiceEndpoint seHttp = host.AddServiceEndpoint(typeof(IService), whbHttp, httpUri);
seHttp.Behaviors.Add(new WebHttpBehavior());

ServiceEndpoint seHttps = host.AddServiceEndpoint(typeof(IService), whbHttps, httpsUri);
seHttps.Behaviors.Add(new WebHttpBehavior());

ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>();
stp.HttpHelpPageEnabled = true;

host.Open(); // <-- Exception: 'System.ServiceModel.AddressAlreadyInUseException' in System.ServiceModel.dll HTTP could not register URL https://+:10901/Service/. Another application has already registered this URL with HTTP.SYS.

I realize that web http is port 80 and https is normally 443, but why am I then reading that http and https can be hosted on the same port? Am i misreading, and its actually not possible? =)

Ted
  • 19,727
  • 35
  • 96
  • 154
  • The port isnt used elsewhere, if I remove the last endpoint, it works. But I also read, linked above, that you can accept both http and https on the same port, ip and computer: _You can do this through the use of Multiple Endpoints. With multiple endpoints you can support the same service over multiple protocols (HTTP and HTTPS in your case)_ – Ted Aug 16 '19 at 06:04
  • Does `new WebServiceHost` take `params` as "second" parameter? In your linked thread it's explicitly `new Uri[] { httpsAddress, httpAddress }` – Markus Deibel Aug 16 '19 at 06:31
  • Yes, it does. `params Uri[] baseAddresses` is the signature, but works with sending in an Array too. – Ted Aug 16 '19 at 06:45

1 Answers1

0

I'm not sure this is what you want. but i just go ahead and post my answer. hosting http and https both on single port is impossible. but you can have both http and https binding like this:

<bindings>
      <webHttpBinding>
        <binding name="RestBinding"/>
        <binding name="SecureRestBinding" >
          <security mode="Transport"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="YOURPROJECT.YOURSERVICE">
        <endpoint address="" behaviorConfiguration="Web" binding="webHttpBinding" bindingConfiguration="RestBinding" name="Rest" contract="YOURPROJECT.IYOURSERVICE"/>
        <endpoint address="" behaviorConfiguration="Web" binding="webHttpBinding" bindingConfiguration="SecureRestBinding" name="SecureRest" contract="YOURPROJECT.IYOURSERVICE"/>
      </service>
    </services>
Mohammad
  • 2,724
  • 6
  • 29
  • 55
  • Yeah, I think the actual answer in my case is "no", http and https on same port cannot be done. Weird is I think I read out there on the net, That WCF could do it. But I guess not. – Ted Aug 18 '19 at 10:46
  • Im not familiar with configuration bindings, and have a hard time understanding what it actually means. What is the effect of the "both http and https binding"? Can the service be accessed by http and https at the same time? Is there a difference in URL in your example above? – Ted Aug 18 '19 at 13:35
  • actually its so simple. you just add the config to your web config and you have both http and https together. but on separated ports. for example you have http://yourservice.com:80 and https://yourservice.com:443 endpoints. also you can have completely different config for each. such as maxBufferSize, maxBufferPoolSize or ... – Mohammad Aug 19 '19 at 07:35