1

我在这里阅读了一些帖子,声称 WCF 可以在同一端点/端口上允许 http 和 https,但这对我来说听起来有点不对劲。我试图设置它,但我无法弄清楚如何以编程方式在同一个端口/ip 上启用 https 和 http 侦听。

尝试添加 https 时出现以下错误:

System.ServiceModel.dll HTTP 中的“System.ServiceModel.AddressAlreadyInUseException”无法注册 URL https://+:10901/Service/。另一个应用程序已经用 HTTP.SYS 注册了这个 URL。

如果我只添加其中一个,效果很好。

代码:

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.

我知道 web http 是 80 端口,https 通常是 443,但是为什么我会读到 http 和 https 可以托管在同一个端口上?我误读了,实际上不可能吗?=)

4

1 回答 1

0

我不确定这是你想要的。但我只是继续发布我的答案。在单个端口上托管 http 和 https 是不可能的。但是您可以像这样同时拥有 http 和 https 绑定:

<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>
于 2019-08-18T10:30:13.690 回答