方法: 信頼されたセッション内のメッセージを変換する

このトピックでは、信頼できるセッションを有効にするために必要な手順について説明します。ここでは、信頼できるセッションを (既定ではなく) オプションでサポートするシステム指定のバインディングを使用します。 信頼できるセッションは、コードを使用して強制的に、または構成ファイルで宣言として有効にすることができます。 この手順では、クライアントとサービスの構成ファイルを使用して、信頼できるセッションを有効にし、送信された順序でメッセージが受信されるように指定します。

この手順の重要な部分は、Binding1 という名前のバインディング構成を参照する bindingConfiguration 属性がエンドポイント構成要素に含まれていることです。 <binding> 構成要素は、この名前を参照して、<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 ファイルを作成し、信頼できるセッションが有効で、メッセージの順序付き配信が必要な WSHttpBinding を使用する CalculatorService のエンドポイントを構成します。

    <?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. インターネット インフォメーション サービス (IIS) 仮想ディレクトリに Service.svc ファイルを配置します。

信頼できるセッションを使用するためにクライアントを 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. クライアントをコンパイルして実行します。

システム指定のバインディングの中には、信頼できるセッションを既定でサポートするものがあります。 次の設定があります。

信頼できるセッションをサポートするカスタム バインディングの作成方法の例については、「方法: カスタムの信頼できるセッションによる HTTPS を使用したバインディングを作成する」を参照してください。

関連項目