【问题标题】:WCF: Programmatically enable both https and http on same ip/portWCF:以编程方式在同一 ip/port 上同时启用 https 和 http
【发布时间】:2019-08-15 23:20:59
【问题描述】:

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

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

'System.ServiceModel.AddressAlreadyInUseException' 在 System.ServiceModel.dll HTTP 无法注册 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 可以托管在同一个端口上?我误读了,实际上不可能吗? =)

【问题讨论】:

  • 该端口未在其他地方使用,如果我删除最后一个端点,它可以工作。但我也读到,上面链接,您可以在同一端口、ip 和计算机上同时接受 http 和 https:您可以通过使用多个端点来做到这一点。使用多个端点,您可以通过多种协议(在您的情况下为 HTTP 和 HTTPS)支持相同的服务
  • new WebServiceHost 是否将params 作为“第二个”参数?在您的链接线程中,它是明确的new Uri[] { httpsAddress, httpAddress }
  • 是的,确实如此。 params Uri[] baseAddresses 是签名,但也适用于发送数组。

标签: c# .net wcf https webservicehost


【解决方案1】:

我不确定这是你想要的。但我只是继续发布我的答案。 在单个端口上托管 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>

【讨论】:

  • 是的,我认为我的实际答案是“否”,同一端口上的 http 和 https 无法完成。奇怪的是我想我在网上读到,WCF 可以做到。但我猜不是。
  • 我不熟悉配置绑定,很难理解它的真正含义。 “http 和 https 都绑定”的效果是什么?服务可以同时通过http和https访问吗?上面的示例中的 URL 有区别吗?
  • 其实就是这么简单。您只需将配置添加到您的 Web 配置中,就可以同时拥有 http 和 https。但在单独的端口上。例如,您有 yourservice.com:80yourservice.com:443 端点。您也可以为每个配置完全不同的配置。例如 maxBufferSize、maxBufferPoolSize 或 ...
猜你喜欢
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-04
  • 1970-01-01
  • 2014-09-23
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多