WSHttpBinding

這個範例示範如何使用 Windows Communication Foundation (WCF) 實作一般服務與一般用戶端。 這個範例是由用戶端主控台程式 (client.exe) 和網際網路資訊服務 (IIS) 所裝載的服務程式庫所組成。 服務會實作定義要求-回覆通訊模式的合約。 合約是由 ICalculator 介面所定義,這個介面會公開數學運算作業 (加、減、乘、除)。 用戶端會對指定的數學運算作業提出同步要求,服務則會以結果回覆。 您可以在主控台視窗中看到用戶端活動。

ms751418.Important(zh-tw,VS.100).gif 注意:
這些範例可能已安裝在您的電腦上。 請先檢查下列 (預設) 目錄,然後再繼續。

<InstallDrive>:\WF_WCF_Samples

如果此目錄不存在,請移至用於 .NET Framework 4 的 Windows Communication Foundation (WCF) 與 Windows Workflow Foundation (WF) 範例 (英文),以下載所有 Windows Communication Foundation (WCF) 和 WF 範例。 此範例位於下列目錄。

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Binding\WS\wsHttp

ms751418.note(zh-tw,VS.100).gif注意:
此範例的安裝程序與建置指示位於本主題的結尾。

這個範例會使用 wsHttpBinding Element 公開 ICalculator 合約。 這個繫結的組態已展開於 Web.config 檔中。

<bindings>
  <wsHttpBinding>
    <!--The following is the expanded configuration section for a-->
    <!--WSHttpBinding. Each property is configured with the default--> 
    <!--value. See the ReliableSession, TransactionFlow, -->
    <!--TransportSecurity, and MessageSecurity samples in the WS -->
    <!--directory to learn how to configure these features. -->
    <binding name="Binding1"
              bypassProxyOnLocal="false" 
              transactionFlow="false" 
              hostNameComparisonMode="StrongWildcard"
              maxBufferPoolSize="524288" 
              maxReceivedMessageSize="65536"
              messageEncoding="Text" 
              textEncoding="utf-8" 
              useDefaultWebProxy="true"
              allowCookies="false">
      <reliableSession ordered="true" 
                       inactivityTimeout="00:10:00"
                       enabled="false" />
      <security mode="Message">
        <message clientCredentialType="Windows" 
                 negotiateServiceCredential="true"
                 algorithmSuite="Default" 
                 establishSecurityContext="true" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

在基底 binding 項目上,maxReceivedMessageSize 值可以讓您設定傳入訊息的大小上限 (以位元組為單位)。 hostNameComparisonMode 值可以讓您設定當分離訊息信號至服務時,是否要考量主機名稱。 messageEncoding 值可以讓您設定訊息是要使用 Text 編碼或 MTOM 編碼。 textEncoding 值可以讓您設定訊息的字元編碼。 bypassProxyOnLocal 值可以讓您設定本機通訊是否要使用 HTTP Proxy。 transactionFlow 值會設定是否流動目前的交易 (如果作業已設定為交易流程)。

<reliableSession> element 項目上,啟用的布林值會設定是否啟用可靠工作階段。 ordered 值會設定是否保留訊息順序。 inactivityTimeout 值會設定工作階段在發生錯誤前能夠閒置的時間長度。

security element of wsHttpBinding 上,mode 值會設定應該使用的安全性模式。 本範例會使用訊息安全性,因此 security element of wsHttpBinding中會指定 message element of wsHttpBinding

當您執行範例時,作業要求和回應會顯示在用戶端主控台視窗中。 在用戶端視窗中按下 ENTER 鍵,即可關閉用戶端。

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

若要安裝、建立及執行範例

  1. 使用下列命令安裝 ASP.NET 4.0。

    %windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable
    
  2. 請確定您已執行 Windows Communication Foundation 範例的單次安裝程序

  3. 若要建立方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例中的指示進行。

  4. 若要在單一或跨電腦的組態中執行本範例,請遵循Running the Windows Communication Foundation Samples中的指示。