次の方法で共有


方法 : コード内でクライアント バインディングを指定する

この例では、電卓サービスを使用するためのクライアントを作成し、そのクライアントのバインディングを強制的にコードで指定します。クライアントは CalculatorService にアクセスします。これにより、ICalculator インターフェイスが実装され、サービスとクライアントの両方で BasicHttpBinding クラスが使用されます。

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

クライアントは 2 つの部分で構成されます。Svcutil.exe によって、ICalculator インターフェイスを実装する ClientCalculator が生成されます。次に、ClientCalculator のインスタンスを作成し、サービスのバインディングとアドレスをコードで指定して、このクライアント アプリケーションを作成します。

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

コード内でカスタム バインディングを指定するには

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

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

    <ServiceContract()> _
    Public Interface ICalculator
    
        <OperationContract()> _
        Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
    
        <OperationContract()> _
        Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
    
    End Interface
    
    [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 も実装します。

    Public Class CalculatorClient
        Inherits System.ServiceModel.ClientBase(Of Microsoft.ServiceModel.Samples.ICalculator)
        Implements Microsoft.ServiceModel.Samples.ICalculator
    
        Public Sub New()
        End Sub
    
        Public Sub New(ByVal endpointConfigurationName As String)
            MyBase.New(endpointConfigurationName)
        End Sub
    
        Public Sub New(ByVal endpointConfigurationName As String, _
                       ByVal remoteAddress As String)
            MyBase.New(endpointConfigurationName, remoteAddress)
        End Sub
    
        Public Sub New(ByVal endpointConfigurationName As String, _
                       ByVal remoteAddress As EndpointAddress)
            MyBase.New(endpointConfigurationName, remoteAddress)
        End Sub
    
        Public Sub New(ByVal binding As Binding, _
                       ByVal remoteAddress As EndpointAddress)
            MyBase.New(binding, remoteAddress)
        End Sub
    
        Public Function Add(ByVal n1 As Double, _
                            ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Add
            Return MyBase.Channel.Add(n1, n2)
        End Function
    
        Public Function Subtract(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Subtract
            Return MyBase.Channel.Subtract(n1, n2)
        End Function
    
        Public Function Multiply(ByVal n1 As Double, _
                                 ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Multiply
            Return MyBase.Channel.Multiply(n1, n2)
        End Function
    
        Public Function Divide(ByVal n1 As Double, _
                               ByVal n2 As Double) As Double Implements Microsoft.ServiceModel.Samples.ICalculator.Divide
            Return MyBase.Channel.Divide(n1, n2)
        End Function
    
    End Class
    
    public 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, EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        {
        }
    
        public CalculatorClient(Binding binding, 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. クライアント アプリケーション内で BasicHttpBinding クラスを使用する ClientCalculator のインスタンスを作成し、指定したアドレスのサービス操作を呼び出します。

        'Client implementation code.
        Friend Class Client
            Shared Sub Main()
    
                'Specify the binding to be used for the client.
                Dim binding As New BasicHttpBinding()
    
                'Specify the address to be used for the client.
                Dim address As New EndpointAddress("https://localhost/servicemodelsamples/service.svc")
    
    
                ' Create a client that is configured with this address and binding.
                Dim client As New CalculatorClient(binding, address)
    
                ' Call the Add service operation.
                Dim value1 = 100.0R
                Dim value2 = 15.99R
                Dim result = client.Add(value1, value2)
                Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result)
    
                ' Call the Subtract service operation.
                value1 = 145.0R
                value2 = 76.54R
                result = client.Subtract(value1, value2)
                Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result)
    
                ' Call the Multiply service operation.
                value1 = 9.0R
                value2 = 81.25R
                result = client.Multiply(value1, value2)
                Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result)
    
                ' Call the Divide service operation.
                value1 = 22.0R
                value2 = 7.0R
                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()
            End Sub
        End Class
    End Namespace
    
        //Client implementation code.
        class Client
        {
            static void Main()
            {
    
                //Specify the binding to be used for the client.
                BasicHttpBinding binding = new BasicHttpBinding();
    
                //Specify the address to be used for the client.
                EndpointAddress address = 
                   new EndpointAddress("https://localhost/servicemodelsamples/service.svc");
    
    
                // Create a client that is configured with this address and binding.
                CalculatorClient client = new CalculatorClient(binding, address);
    
                // 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();
            }
        }
    }
    
    
  5. クライアントをコンパイルして実行します。

参照

概念

サービスとクライアントを構成するためのバインディングの使用