共用方式為


自訂綁定可靠連線會話

ReliableSession 範例示範如何使用各種傳輸和訊息編碼元素來設定自定義系結,特別是啟用可靠的會話。 自定義系結是由離散綁定項的已排序列表所定義。

範例詳細資料

可靠的會話提供可靠傳訊和會話的功能。 可靠的訊息傳輸在失敗時會重試通訊,並允許指定訊息的順序到達等傳遞保證。 會話會在呼叫之間維護客戶端的狀態。 此範例會實作維護客戶端狀態的會話,並指定順序傳遞保證。 此範例是以實作計算機服務的 用戶入門 為基礎。 可靠的會話功能會在用戶端和服務的應用程式組態檔中啟用和設定。

備註

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

綁定項的順序對於定義自定義系結很重要,因為每個系結都代表通道堆疊中的圖層(請參閱 自定義系結)。

範例的服務組態定義如下的程式代碼範例所示。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service
          name="Microsoft.ServiceModel.Samples.CalculatorService"
          behaviorConfiguration="CalculatorServiceBehavior">
        <!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc  -->
        <!-- specify customBinding binding and a binding configuration to use -->
        <endpoint address=""
                  binding="customBinding"
                  bindingConfiguration="Binding1"
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />
        <!-- The mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex -->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>

    <!-- custom binding configuration - configures HTTP transport, reliable sessions -->
    <bindings>
      <customBinding>
        <binding name="Binding1">
          <reliableSession />
          <security authenticationMode="SecureConversation"
                     requireSecurityContextCancellation="true">
          </security>
          <compositeDuplex />
          <oneWay />
          <textMessageEncoding messageVersion="Soap12WSAddressing10" writeEncoding="utf-8" />
          <httpTransport authenticationScheme="Anonymous" bypassProxyOnLocal="false"
                        hostNameComparisonMode="StrongWildcard"
                        proxyAuthenticationScheme="Anonymous" realm=""
                        useDefaultWebProxy="true" />
        </binding>
      </customBinding>
    </bindings>

    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>

</configuration>

在跨計算機案例中執行時,您必須變更用戶端的端點位址,以反映服務的主機名。

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

    這很重要

    在跨計算機組態中執行用戶端時,請務必將 address 屬性和 <>中的 “localhost” 取代為適當計算機的名稱,如下列範例所示。

    <endpoint name = ""
    address="http://service_machine_name/servicemodelsamples/service.svc" />
    <compositeDuplex clientBaseAddress="http://client_machine_name:8000/myClient/" />