【问题标题】:HTTP & HTTP WCF ServicesHTTP 和 HTTP WCF 服务
【发布时间】:2011-06-22 02:25:03
【问题描述】:

我能否拥有一个同时具有 HTTP(基本 http 绑定)和 HTTPS(基本 http 绑定)绑定的 WCF 服务项目?例如,我会:

https://localhost:44303/ServiceA.svc http://localhost:12345/ServiceB.svc

将它们放入单独的服务项目(以及在我们部署应用程序时的单独站点)中是否有任何好处?

【问题讨论】:

    标签: c# .net wcf wcf-binding wcf-security


    【解决方案1】:

    如果您已经拥有 HTTP 绑定,则无需更改代码即可添加 HTTPS 绑定。这是 WCF 的一大优势。您只需在配置文件中添加一个新端点,而不是添加单独的站点。

    以下是同时使用 HTTP 和 HTTPS 的配置示例。
    可以看到有两个命名绑定:notSecureBinding和secureBinding,分别对应HTTP和HTTPS。

      <bindings>
        <basicHttpBinding>
          <binding name="notSecureBinding"
                   maxBufferSize="2147483647"
                   maxReceivedMessageSize="2147483647">
            <security mode="None"/>
          </binding>
          <binding name="secureBinding"
                   maxBufferSize="2147483647"
                   maxReceivedMessageSize="2147483647">
            <security mode="Transport">
              <transport clientCredentialType="None"/>
            </security>
          </binding>
        </basicHttpBinding>
      </bindings>
      <behaviors>
        <serviceBehaviors>
          <behavior name="StandardServiceBehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
            <serviceAuthorization principalPermissionMode="None"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <services>
        <service behaviorConfiguration="StandardServiceBehavior"
                 name="ServiceName">
          <endpoint address=""
                    binding="basicHttpBinding"
                    bindingConfiguration="notSecureBinding"
                    contract="Namespace.IService"/>
          <endpoint address=""
                    binding="basicHttpBinding"
                    bindingConfiguration="secureBinding"
                    contract="Namespace.IService"/>
          <endpoint address="mex"
                    binding="mexHttpBinding"
                    contract="IMetadataExchange"/>
        </service>
      </services>
    

    【讨论】:

    • @user472292 - 你在 IIS 上托管吗?您是否创建了 SSL 证书?您是否将证书绑定到端口?
    • 如果您需要能够通过 https 端点检索元数据,您需要将 httpsGetEnabled="true" 添加到 serviceMetadata 元素。您还需要再添加一个 mex 端点。
    • @user472292 - 我展示的配置来自我的生产项目,它正在工作。
    【解决方案2】:

    我尝试了这个,当我尝试使用我的安全服务时,我收到以下错误:

     The HTML document does not contain Web service discovery information. Metadata contains a reference that cannot be resolved: 'https://localhost:44304/ExternalOrderProcessing.svc'. There was no endpoint listening at https://localhost:44304/ExternalOrderProcessing.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. The remote server returned an error: (404) Not Found.If the service is defined in the current solution, try building the solution and adding the service reference again.
    

    当我尝试使用我的不安全服务时,我收到以下错误:

     The HTML document does not contain Web service discovery information. Metadata contains a reference that cannot be resolved: 'http://localhost:5000/LegacyOrderProcessing.svc'. Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:5000/LegacyOrderProcessing.svc.  The client and service bindings may be mismatched. The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.. If the service is defined in the current solution, try building the solution and adding the service reference again.
    

    我在 IIS Express 中运行它。我已将项目设置为允许 SSL。我的配置如下:

    <services>
      <service name="ExternalOrderProcessing" behaviorConfiguration="SecureBehavior">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBindingSecure" contract="IExternalOrderProcessing" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
      <service name="LegacyOrderProcessing" behaviorConfiguration="UnsecureBehavior">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="ILegacyOrderProcessing" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    
    <behaviors>
      <serviceBehaviors>
        <behavior name="SecureBehavior">
          <serviceMetadata httpsGetEnabled="true" httpsGetUrl=""/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceCredentials>
            <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" />
            <clientCertificate>
              <authentication certificateValidationMode="None" />
            </clientCertificate>
          </serviceCredentials>
        </behavior>
        <behavior name="UnsecureBehavior">
          <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    <bindings>
    
      <basicHttpBinding>
        <!-- Used by external order processing service -->
        <binding name="BasicHttpBindingSecure"
                hostNameComparisonMode="StrongWildcard"
                maxBufferSize="2147483647"
                maxReceivedMessageSize="2147483647"
                receiveTimeout="00:05:00"
                sendTimeout="00:05:00"
                openTimeout="00:05:00"
                closeTimeout="00:05:00">
          <readerQuotas maxArrayLength="2147483647"/>
          <security mode="TransportWithMessageCredential">
            <transport clientCredentialType="Certificate" proxyCredentialType="None" realm="" />
            <message clientCredentialType="Certificate" algorithmSuite="Default"  />
          </security>
        </binding>
        <!-- Used to create binding to internal order processing service -->
        <binding name="BasicHttpBinding"
                hostNameComparisonMode="StrongWildcard"
                maxBufferSize="2147483647"
                maxReceivedMessageSize="2147483647"
                receiveTimeout="00:05:00"
                sendTimeout="00:05:00"
                openTimeout="00:05:00"
                closeTimeout="00:05:00">
          <readerQuotas maxArrayLength="2147483647"/>
          <security mode="None" />
        </binding>
      </basicHttpBinding>
    
    </bindings>
    
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    

    如果我将服务放入两个单独的项目中,它就可以工作。当我这样做时,我省略了配置中的服务部分并删除了 name="BasicHttpBindingSecure" 和 name="SecureBehavior"。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 2011-01-02
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      相关资源
      最近更新 更多