共用方式為


WSHttpBinding

wsHttp 範例示範如何使用 Windows Communication Foundation (WCF) 實作一般服務和一般用戶端。 此範例包含用戶端控制台程式(client.exe),以及由 Internet Information Services (IIS) 裝載的服務連結庫。 服務會實作定義要求-回復通訊模式的合約。 合約是由 ICalculator 介面所定義,其會公開數學運算(加、減、乘和除)。 用戶端會向指定的數學運算提出同步要求,而服務會以結果回復。 主控台視窗中會顯示客戶端活動。

備註

此範例的安裝程式和建置指示位於本主題結尾。

此範例會使用 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> 元素上,啟用的布爾值會設定是否啟用可靠會話。 值 ordered 會設定是否保留訊息順序。 值 inactivityTimeout 會設定會話在發生錯誤之前可以閑置多久的時間。

<安全性>方面,mode值會設定應該使用哪一種安全模式。 在此範例中,會使用訊息安全性,這就是為什麼<在安全性內指定訊息>的原因<。>

當您執行範例時,作業要求和回應會顯示在用戶端控制台視窗中。 在客戶端視窗中按 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 範例 執行One-Time 安裝程式。

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

  4. 若要在單一或跨計算機組態中執行範例,請遵循執行 Windows Communication Foundation 範例 中的指示。