自訂繫結可靠工作階段

自訂繫結由經過排序的獨立繫結項目清單所定義。此範例示範如何使用各種傳輸與訊息編碼項目設定自訂繫結,特別是啟用可靠工作階段。

可靠的工作階段會提供可靠傳訊和工作階段功能。可靠的傳訊失敗時會重試通訊,而且允許指定傳遞保證,例如訊息依序到達。工作階段會保持呼叫之間的用戶端狀態。此範例會實作維持用戶端狀態的工作階段,並且指定依序傳遞保證。此範例以實作計算機服務的使用者入門範例為基礎。可靠工作階段的功能已在用戶端和服務的應用程式組態檔中啟用和設定。

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

繫結項目的排序對於定義自訂繫結很重要,因為每一個項目都代表通道堆疊中的一層 (請參閱Custom Bindings)。

範例的服務組態定義如下:

<?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: https://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 https://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. 請確定您已執行 Windows Communication Foundation 範例的單次安裝程序

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

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

    ms752232.Important(zh-tw,VS.90).gif 注意:
    在跨電腦組態中執行用戶端時,請確定 Service Endpoint 項目的 address 屬性和 compositeDuplex elementclientBaseAddress 屬性中的 "localhost" 都已取代為適當的電腦名稱,如下列範例所示:

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

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.