【问题标题】:Wcf HTTP and HTTPS on the same host/portWcf HTTP 和 HTTPS 在同一主机/端口上
【发布时间】:2014-04-10 15:16:14
【问题描述】:

你好,

我知道如何为 http 或 https 创建一个自托管的 wcf,但不是同时。

我想要这 2 个网址的 wcf:

  1. https://127.0.0.1:13070/ProxySips/
  2. http://127.0.0.1:13070/ProxySips/

目前我有 https 的配置(带有证书:makecert + netsh),它工作正常:

app.config

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="basicHttp" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <security mode="Transport">
        <transport clientCredentialType="None"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service name="ProxySips_Wcf.Sips" behaviorConfiguration="ProxySips_Wcf.ProxySipsBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="https://127.0.0.1:13070/ProxySips/"   />
      </baseAddresses>
    </host>
    <endpoint address="" binding="basicHttpBinding"
              bindingConfiguration="basicHttp"
              contract="ProxySips_Wcf.ISips"/>
    <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ProxySips_Wcf.ProxySipsBehavior">
      <serviceMetadata  httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="True" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </serviceBehaviors>
</behaviors>

主持人

var serviceType = typeof(ProxySips_Wcf.Sips);
var host = new ServiceHost(serviceType);
host.Open();

可以帮我设置同一个地址的http版本吗?

非常感谢

【问题讨论】:

  • 这个问题解决了吗?我试图弄清楚 WCF 是否可以同时在同一个 URI/端口上侦听 http 和 https。到目前为止,我还没有成功。

标签: wcf wcf-binding wcf-security


【解决方案1】:

您可以通过使用Multiple Endpoints 来做到这一点。使用多个端点,您可以通过多种协议(在您的情况下为 HTTP 和 HTTPS)支持相同的服务。

所以你需要添加以下ServiceBehavior

<behavior name="MyUnsecureServiceBehavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
     <dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>

然后下面Binding

<basicHttpBinding>
  <binding name= MyUnsecureBindingConfig"
                 maxBufferPoolSize="2147483647" 
                 maxReceivedMessageSize="2147483647" 
                 messageEncoding="Text">
     <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
                   maxArrayLength="2147483647" maxBytesPerRead="2147483647" 
                   maxNameTableCharCount="2147483647" />
     <security mode="None">
         <transport clientCredentialType="None" />
         <message establishSecurityContext="false" />
     </security>
</basicHttpBinding>

最后是以下Address 配置:

<service behaviorConfiguration="MyUnsecureServiceBehavior"
                         name="MyUnsecureService">
   <endpoint address="http://127.0.0.1:13070/ProxySips/"
             binding="basicHttpBinding"
             contract="ProxySips_Wcf.ISips"
             bindingConfiguration="MyUnsecureBindingConfig" />
   <endpoint address="mex"
             binding="mexHttpBinding"
             contract="IMetadataExchange"/>
</service>

更新:

在您的WCF Host 应用程序中,您需要指定新的URI 来监听。您可以像这样设置您的主机:

var httpsAddress = new Uri("https:// 127.0.0.1:13070/ProxySips/");
var httpAddress = new Uri("http://127.0.0.1:13070/ProxySips/");
var host = new ServiceHost(typeof(ProxySips_Wcf.Sips), 
                            new Uri[] { httpsAddress, httpAddress });
host.Open();

【讨论】:

  • 您好 Derek,感谢您的帮助,但是当我放置您的配置时,执行时没有错误,但 http 端点上没有任何反应。我应该创建一个新的主机服务吗?
  • @Bob:我更新了我的答案,说明如何为您的主机应用程序指定要监听的多个端点。无论哪种方式都让我知道。
猜你喜欢
  • 2014-04-22
  • 2011-02-04
  • 1970-01-01
  • 2019-02-03
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多