次の方法で共有


方法: 構成でクライアント バインドを指定する

この例では、電卓サービスを使用するクライアント コンソール アプリケーションが作成され、そのクライアントのバインドが構成で宣言によって指定されます。 クライアントは、CalculatorService インターフェイスを実装するICalculatorにアクセスし、サービスとクライアントの両方で BasicHttpBinding クラスを使用します。

説明されている手順では、電卓サービスが実行されていることを前提としています。 サービスを構築する方法については、「 方法: 構成でサービス バインドを指定する」を参照してください。 また、Windows Communication Foundation (WCF) が提供する ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を使用して、クライアント コンポーネントを自動的に生成します。 このツールは、サービスにアクセスするためのクライアント コードと構成を生成します。

クライアントは 2 つの部分で構築されています。 Svcutil.exe は、ClientCalculator インターフェイスを実装するICalculatorを生成します。 このクライアント アプリケーションは、 ClientCalculatorのインスタンスを構築することによって構築されます。

通常は、コード内で命令型ではなく、構成でバインディングとアドレス情報を宣言的に指定することをお勧めします。 通常、コードでのエンドポイントの定義は実用的ではありません。デプロイされたサービスのバインドとアドレスは、通常、サービスの開発中に使用されるものとは異なるためです。 より一般的には、バインディングとアドレス指定の情報をコードから除外することで、アプリケーションを再コンパイルまたは再デプロイしなくても変更できます。

構成 エディター ツール (SvcConfigEditor.exe) を使用して、次のすべての構成手順を実行できます。

この例のソース コピーについては、 BasicBinding サンプルを 参照してください。

構成でのクライアント バインドの指定

  1. コマンド ラインの Svcutil.exe を使用して、サービス メタデータからコードを生成します。

    Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address>
    
  2. 生成されるクライアントには、クライアント実装が満たす必要があるサービス コントラクトを定義する ICalculator インターフェイスが含まれています。

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [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);
    }
    
    [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);
    }
    
    
  3. 生成されたクライアントには、 ClientCalculatorの実装も含まれています。

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    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);
        }
    }
    
    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;
       }
    }
    
    
  4. Svcutil.exe では、 BasicHttpBinding クラスを使用するクライアントの構成も生成されます。 Visual Studio を使用する場合は、このファイルに App.config名前を付けます。アドレスとバインディング情報は、サービスの実装内のどこにも指定されないことに注意してください。 また、構成ファイルからその情報を取得するためにコードを記述する必要はありません。

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
    
        <client>
          <endpoint 
              name=""
              address="http://localhost/servicemodelsamples/service.svc" 
              binding="basicHttpBinding" 
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
        </client>
    
        <bindings>
          <basicHttpBinding/>
        </bindings>
    
      </system.serviceModel>
    
    </configuration>
    
  5. アプリケーションで ClientCalculator のインスタンスを作成し、サービス操作を呼び出します。

    
    using System;
    using System.ServiceModel;
    
    namespace Microsoft.ServiceModel.Samples
    {
    
        //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. クライアントをコンパイルして実行します。

こちらもご覧ください