Wcf HTTP和HTTPS在同一主机/端口上

时间:2014-04-10 15:16:15

标签: wcf wcf-binding wcf-security

您好,

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

我想为这2个网址添加一个wcf:

  1. https:// 127.0.0.1:13070/ProxySips /
  2. http:// 127.0.0.1:13070/ProxySips /
  3. 目前我已经配置了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版本吗?

    非常感谢

1 个答案:

答案 0 :(得分:0)

您可以通过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();