如何:在可靠会话中交换消息

本主题概述了使用支持此类会话但默认情况下不支持此类会话的系统提供的绑定之一启用可靠会话所需的步骤。 必须在配置文件中使用代码或以声明方式启用可靠会话。 此过程使用客户端和服务配置文件来启用可靠会话,并规定消息按发送的顺序相同。

此过程的关键部分是终结点配置元素中包含一个属性bindingConfiguration,该属性引用了一个名为Binding1的绑定配置。 绑定<>配置元素引用此名称,通过将 <reliableSession>元素的enabled属性设置为true来启用可靠会话。 通过将ordered属性设置为true,您可以指定可靠会话的有序传递保证。

有关此示例的源副本,请参阅 WS Reliable Session

使用 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 文件,为使用 WSHttpBindingCalculatorService 配置终结点,启用可靠会话并按顺序传递所需的消息

    <?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 文件放置在 Internet Information Services (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({value1},{value2}) = {result}");
    
            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine($"Subtract({value1},{value2}) = {result}");
    
            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine($"Multiply({value1},{value2}) = {result}");
    
            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine($"Divide({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. 编译并运行客户端。

示例

默认情况下,系统提供的多个绑定支持可靠的会话。 这些包括:

有关如何创建支持可靠会话的自定义绑定的示例,请参阅 如何:使用 HTTPS 创建自定义可靠会话绑定

另请参阅