【发布时间】: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