C# WCF:以编程方式在同一ip/端口上同时启用https和http

C# WCF:以编程方式在同一ip/端口上同时启用https和http,c#,.net,wcf,https,webservicehost,C#,.net,Wcf,Https,Webservicehost,我在这里读过,声称WCF可以在同一个端点/端口上允许http和https,但这听起来有点错误。我试图设置它,但我不知道如何以编程方式在同一端口/ip上启用https和http侦听 尝试添加https时,出现以下错误: 中的“System.ServiceModel.AddressalReadyUseException” System.ServiceModel.dll HTTP无法注册URL . 另一个应用程序已经启动 已向HTTP.SYS注册此URL 如果我只添加其中一个,效果很好 代码: Uri

我在这里读过,声称WCF可以在同一个端点/端口上允许http和https,但这听起来有点错误。我试图设置它,但我不知道如何以编程方式在同一端口/ip上启用https和http侦听

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

中的“System.ServiceModel.AddressalReadyUseException” System.ServiceModel.dll HTTP无法注册URL . 另一个应用程序已经启动 已向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.
urihttpsuri=newuri(“https://“+localIp.ToString()+”:“+settings.\u WebservicePort.ToString()+”/Service/”;
Uri httpUri=新Uri(“http://“+localIp.ToString()+”:“+settings.\u WebservicePort.ToString()+”/Service/”;
主机=新的WebServiceHost(类型(服务)、httpUri、httpsUri);
WebHttpBinding whbHttp=新的WebHttpBinding
{
CrossDomainScriptAccessEnabled=true,
安全性={Mode=WebHttpSecurityMode.None},
MaxReceivedMessageSize=10000000
};
WebHttpBinding whbHttps=新的WebHttpBinding
{
CrossDomainScriptAccessEnabled=true,
安全性={Mode=WebHttpSecurityMode.Transport},
MaxReceivedMessageSize=10000000
};
ServiceEndpoint seHttp=host.AddServiceEndpoint(typeof(IService)、whbHttp、httpUri);
添加(新的WebHttpBehavior());
ServiceEndpoint seHttps=host.AddServiceEndpoint(typeof(IService)、whbHttps、httpsUri);
seHttps.Behaviors.Add(新的WebHttpBehavior());
ServiceDebugBehavior stp=host.Description.Behaviors.Find();
stp.HttpHelpPageEnabled=true;

host.Open();// 我不确定这是你想要的。但我只是继续写我的答案。 在单个端口上同时托管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是不可能的。但您可以同时使用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>


端口不在其他地方使用,如果我删除最后一个端点,它就可以工作。但我也阅读了上面的链接,您可以在同一端口、ip和计算机上接受http和https:您可以通过使用多个端点来实现这一点。使用多个端点,您可以通过多个协议(在您的情况下为HTTP和HTTPS)支持同一服务
new WebServiceHost
是否将
params
作为“第二个”参数?在链接线程中,它是显式的
newuri[]{httpsAddress,httpAddress}
是的,它是
params Uri[]baseAddresses
是签名,但也可以在数组中发送。该端口不在其他地方使用,如果我删除最后一个端点,它就可以工作。但我也阅读了上面的链接,您可以在同一端口、ip和计算机上接受http和https:您可以通过使用多个端点来实现这一点。使用多个端点,您可以通过多个协议(在您的情况下为HTTP和HTTPS)支持同一服务
new WebServiceHost
是否将
params
作为“第二个”参数?在链接线程中,它是显式的
newuri[]{httpsAddress,httpAddress}
是的,它是
params Uri[]baseAddresses
是签名,但也可以在数组中发送。是的,我认为在我的情况下,实际答案是“否”,无法在同一端口上执行http和https。奇怪的是,我想我在网上读到,WCF可以做到这一点。我不熟悉配置绑定,很难理解它的真正含义。“http和https绑定”的效果是什么?可以同时通过http和https访问服务吗?在上面的例子中,URL有什么不同吗?其实很简单。您只需将配置添加到web配置中,就可以同时使用http和https。但是在不同的港口。例如,您有和端点。此外,您还可以为每个配置完全不同。比如maxBufferSize、maxBufferPoolSize或者……是的,我认为我的实际答案是“否”,不能在同一端口上使用http和https。奇怪的是,我想我在网上读到,WCF可以做到这一点。我不熟悉配置绑定,很难理解它的真正含义。“http和https绑定”的效果是什么?可以同时通过http和https访问服务吗?在上面的例子中,URL有什么不同吗?其实很简单。您只需将配置添加到web配置中,就可以同时使用http和https。但是在不同的港口。例如,您有和端点。此外,您还可以为每个配置完全不同。例如maxBufferSize、maxBufferPoolSize或。。。