我在这里阅读了一些帖子,声称 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 可以托管在同一个端口上?我误读了,实际上不可能吗?=)