共用方式為


作法:在可靠的工作階段內交換訊息

本主題概要說明透過其中一個系統提供的繫結啟用可靠工作階段 (此繫結支援此類工作階段,但非預設) 所需的步驟。 您可以透過命令式程式碼或是宣告式組態檔,來啟用可靠工作階段。 本程序透過用戶端組態檔與服務組態檔來啟用可靠工作階段,並規定訊息以當初傳送的相同順序依序抵達。

此程序的重要部分在於端點設定元素包含bindingConfiguration屬性,而此屬性參考了名為Binding1的繫結設定。 <繫結>設定元素會參考這個名稱,藉由<reliableSession>元素的enabled屬性設定為true來啟用可靠的工作階段。 您可以將 ordered 屬性設為 true,為可靠工作階段指定依序傳遞保證。

如需這個範例的來源複本,請參閱 WS 可靠工作階段

將包含 WSHttpBinding 的服務設為使用可靠工作階段

  1. 定義服務類型的服務合約。

    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }
    
  2. 在服務類別中實作服務合約。 請注意,服務的實作內並未指定位址或繫結資訊。 您不需要撰寫程式碼,即可從組態檔擷取位址或繫結資訊。

    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }
        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }
        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    }
    
  3. 建立 Web.config 檔案為CalculatorService設定端點,這個端點會使用已啟用可靠工作階段並要求訊息依序傳遞的WSHttpBinding

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
    
        <services>
          <service name="Microsoft.ServiceModel.Samples.CalculatorService">
    
            <!--
                 This endpoint is exposed at the base address provided by
                 host: http://localhost/servicemodelsamples/service.svc
    
                 Specify wsHttpBinding binding and a binding configuration
                 to use
            -->
            <endpoint address=""
                      binding="wsHttpBinding"
                      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>
    
        <!--
             Configures WSHttpBinding for reliable sessions with ordered
             delivery.
        -->
        <bindings>
          <wsHttpBinding>
            <binding name="Binding1">
              <reliableSession enabled="true" ordered="true" />
            </binding>
          </wsHttpBinding>
        </bindings>
    
      </system.serviceModel>
    </configuration>
    
  4. 建立包含此行的 Service.svc 檔案:

    <%@ServiceHost language=c# Service="CalculatorService" %>
    
  5. Service.svc 檔放入您的網際網路資訊服務 (IIS) 虛擬目錄中。

將包含 WSHttpBinding 的用戶端設為使用可靠工作階段

  1. 從命令列使用 ServiceModel 中繼資料公用程式工具 (Svcutil.exe),可從服務中繼資料產生程式碼:

    Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address>
    
  2. 所產生的用戶端會包含ICalculator介面,其中定義了用戶端實作所必須滿足的服務合約。

    //Generated interface defining the ICalculator contract	
    [System.ServiceModel.ServiceContractAttribute(
    Namespace="http://Microsoft.ServiceModel.Samples",
    ConfigurationName="Microsoft.ServiceModel.Samples.ICalculator")]
    public interface ICalculator
    {
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Add",
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]
        double Add(double n1, double n2);
    
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract",
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]
        double Subtract(double n1, double n2);
    
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply",
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]
        double Multiply(double n1, double n2);
    
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide",
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]
        double Divide(double n1, double n2);
    }
    
  3. 產生的用戶端應用程式也包含 ClientCalculator 的實作。 請注意,服務的實作內部並未指定位址和繫結資訊。 您不需要撰寫程式碼,即可從組態檔擷取位址或繫結資訊。

    // Implementation of the CalculatorClient
    public partial class CalculatorClient :
        System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>,
        Microsoft.ServiceModel.Samples.ICalculator
    {
        public CalculatorClient()
        {
        }
    
        public CalculatorClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
        }
    
        public CalculatorClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }
    
        public CalculatorClient(string endpointConfigurationName,
            System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }
    
        public CalculatorClient(System.ServiceModel.Channels.Binding binding,
            System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
        }
    
        public double Add(double n1, double n2)
        {
            return base.Channel.Add(n1, n2);
        }
    
        public double Subtract(double n1, double n2)
        {
            return base.Channel.Subtract(n1, n2);
        }
    
        public double Multiply(double n1, double n2)
        {
            return base.Channel.Multiply(n1, n2);
        }
    
        public double Divide(double n1, double n2)
        {
            return base.Channel.Divide(n1, n2);
        }
    }
    
  4. Svcutil.exe 也會為使用WSHttpBinding類別的用戶端產生設定。 使用 Visual Studio 時,將組態檔命名為 App.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
    
        <client>
          <!-- 
            Specify wsHttpBinding binding and a binding configuration 
            to use
          -->
          <endpoint 
              address="http://localhost/servicemodelsamples/service.svc" 
              binding="wsHttpBinding" 
              bindingConfiguration="Binding1" 
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
        </client>
    
        <!-- 
          Configures WSHttpBinding for reliable sessions with ordered 
          delivery
        -->
        <bindings>
          <wsHttpBinding>
            <binding name="Binding1">
              <reliableSession enabled="true" ordered="true" />
            </binding>
          </wsHttpBinding>
        </bindings>
    
      </system.serviceModel>
    </configuration>
    
  5. 在應用程式內建立ClientCalculator的執行個體,並呼叫服務作業。

    //Client implementation code.
    class Client
    {
        static void Main()
        {
            // Create a client with given client endpoint configuration
            CalculatorClient client = new CalculatorClient();
    
            // Call the Add service operation.
            double value1 = 100.00D;
            double value2 = 15.99D;
            double result = client.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
    
            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
    
            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
    
            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
    
            //Closing the client gracefully closes the connection and cleans up resources
            client.Close();
    
            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
    
  6. 請編譯並執行用戶端。

範例

好幾個系統提供的繫結預設都支援可靠工作階段。 包括:

如需如何建立支援可靠工作階段的自訂繫結範例,請參閱HOW TO:使用 HTTPS 建立自訂可靠工作階段繫結

另請參閱