2017年9月2日 星期六

糟糕,HTTP轉HTTPS WCF Web服務失效了!

一、問題描述:

Microsoft WCF(Windows Communication Foundation)可以說是SOA 最佳實作技術,最主要的概念包含『鬆散偶合力』 以及 『軟體服務』。而SOA全名為 Service Oriented Architecture。這些名詞和概念不是本文的重點,讀者可以自行在網路上查到相關資料。

今天的問題是建置使用WCF服務的網站,如果由HTTP轉為HTTPS,預設情況下WCF服務是無法正常運作的,必須調整Web.config的serviceModel設定,讓我們來看看該如何調整?

二、實作解決方案:

以下是可正常運作的設定:

<system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingConfig">
        </binding>
        <binding name="webHttpsBindingConfig">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>

      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="behaviorName">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="CmsP.Ctrl.Buyone.BuyoneWCF">
        <endpoint address="" behaviorConfiguration="behaviorName" bindingConfiguration="webHttpBindingConfig" binding="webHttpBinding" contract="mycontract" />
        <endpoint address="" behaviorConfiguration="behaviorName" bindingConfiguration="webHttpsBindingConfig" binding="webHttpBinding" contract="mycontract" />
      </service>
    </services>
  </system.serviceModel>

 ※標示紅色字體部分為HTTPS運作的相關設定

沒有留言: