方法 : Windows Communication Foundation クライアントを作成する

これは、基本的な Windows Communication Foundation (WCF) サービスとそのサービスを呼び出すことができるクライアントの作成に必要な 6 つのタスクのうち、4 番目のタスクです。6 つのすべてのタスクの概要については、「チュートリアル入門」を参照してください。

ここでは、WCF サービスからメタデータを取得し、このメタデータを使用して、サービスにアクセスできる WCF クライアント プロキシを作成する方法について説明します。このタスクは、WCF によって提供される ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を使用することで完了できます。このツールは、サービスからメタデータを取得し、ユーザーが選択した言語でプロキシのマネージ ソース コード ファイルを生成します。このツールでは、クライアント プロキシだけでなく、クライアントの構成ファイルも作成します。この構成ファイルにより、クライアント アプリケーションはエンドポイントのいずれかにあるサービスに接続できるようになります。

ms733133.note(ja-jp,VS.100).gif注 :
ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を使用する代わりに、サービス参照を Visual Studio 2010 内のクライアント プロジェクトに追加して、クライアント プロキシを作成できます。

ms733133.Warning(ja-jp,VS.100).gif 注意 :
Visual Studio 2010 のクラス ライブラリ プロジェクトから WCF サービスを呼び出すときは、サービス参照の追加機能を使用して、プロキシおよび関連構成ファイルを自動的に生成できます。この構成ファイルはクラス ライブラリ プロジェクトで使用されません。クラス ライブラリを呼び出す実行可能ファイルを格納するディレクトリに、構成ファイルをコピーする必要があります。

クライアント アプリケーションは、生成されたプロキシを使用して WCF クライアント オブジェクトを作成します。この手順については、「方法 : Windows Communication Foundation クライアントを使用する」を参照してください。

手順に続く例で、このタスクで生成されるクライアントのコードを示します。

Windows Communication Foundation クライアントを作成するには

  1. 次の手順に従って、現在のソリューション内に Visual Studio 2010 のクライアントの新しいプロジェクトを作成します。

    1. ソリューション エクスプローラー (右上) で、サービスを含む同じソリューション内の現在のソリューションを右クリックし、[追加][新しいプロジェクト] の順に選択します。

    2. [新しいプロジェクトの追加] ダイアログ ボックスで、[Visual Basic] または [Visual C#] を選択し、[コンソール アプリケーション] テンプレートをクリックして、「Client」という名前を付けます。既定の [場所] を使用します。

    3. [OK] をクリックします。

  2. プロジェクトで System.ServiceModel.dll の参照を追加します。

    1. ソリューション エクスプローラーで、[クライアント] プロジェクトの下にある [参照設定] フォルダーを右クリックし、[参照の追加] を選択します。

    2. [.NET] タブを選択し、ボックスの一覧から [System.ServiceModel.dll] (バージョン 4.0.0.0) を選択して [OK] をクリックします。

    ms733133.note(ja-jp,VS.100).gif注 :
    コマンド ライン コンパイラ (Csc.exe や Vbc.exe など) を使用する場合は、アセンブリへのパスも入力する必要があります。たとえば、Windows Vista を実行しているコンピューターの場合、既定のパスは、Windows\Microsoft.NET\Framework\v4.0 になります。

  3. Program.cs ファイルまたは Program.vb ファイルで、System.ServiceModel 名前空間の using ステートメント (Visual Basic の Imports) を追加します。

    Imports System.ServiceModel
    
    using System.ServiceModel;
    
  4. Visual Studio では、前のトピックで作成されたサービスを開始します。詳細については、次のトピックを参照してください。 方法 : 基本的な Windows Communication Foundation サービスをホストおよび実行する.

  5. ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を適切なスイッチと共に実行して、クライアント コードと構成ファイルを次の手順に従って作成します。

    1. [スタート] メニューの [すべてのプログラム] をクリックし、[Visual Studio 2010] をクリックします。[Visual Studio ツール] をクリックし、[Visual Studio 2010 コマンド プロンプト] をクリックします。

    2. クライアント コードを格納するディレクトリに移動します。既定値を使用してクライアント プロジェクトを作成した場合、このディレクトリは C:\Users\<user name>\My Documents\Visual Studio 10\Projects\Service\Client です。

    3. コマンド ライン ツールである ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を適切なスイッチと共に使用して、クライアント コードを作成します。次の例では、サービスのコード ファイルと構成ファイルを生成しています。

      svcutil.exe /language:vb /out:generatedProxy.vb /config:app.config https://localhost:8000/ServiceModelSamples/service
      
      svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config https://localhost:8000/ServiceModelSamples/service
      

      既定では、クライアント プロキシ コードは、サービスの名前にプログラミング言語の適切な拡張子 (Visual Basic の場合は .vb、C# の場合は .cs) が付けられたファイルに生成されます (この例では、CalculatorService.cs または CalculatorService.vb)。/out スイッチは、クライアント プロキシ ファイルの名前を GeneratedProxy.cs に変更します。また、/config スイッチは、クライアント構成ファイルの名前を既定の Output.config から App.config に変更します。この 2 つのファイルは、C:\Users\<user name>\My Documents\Visual Studio 10\Projects\Service\Client ディレクトリに生成されます。

  6. Visual Studio で、生成されたプロキシをクライアント プロジェクトに追加します。ソリューション エクスプローラーで、クライアント プロジェクトを右クリックし、[追加] をポイントし、[既存の項目] をクリックします。前の手順で生成された generatedProxy ファイルを選択します。

次の例は、ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) で生成されたクライアント コードを示しています。

'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:2.0.50727.1366
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict Off
Option Explicit On



<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0"),  _
 System.ServiceModel.ServiceContractAttribute([Namespace]:="http://Microsoft.ServiceModel.Samples", ConfigurationName:="ICalculator")>  _
Public Interface ICalculator
    
    <System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/ICalculator/Add", ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")>  _
    Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
    
    <System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")>  _
    Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
    
    <System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")>  _
    Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
    
    <System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/ICalculator/Divide", ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")>  _
    Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
End Interface

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")>  _
Public Interface ICalculatorChannel
    Inherits ICalculator, System.ServiceModel.IClientChannel
End Interface

<System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")>  _
Partial Public Class CalculatorClient
    Inherits System.ServiceModel.ClientBase(Of ICalculator)
    Implements ICalculator
    
    Public Sub New()
        MyBase.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 System.ServiceModel.EndpointAddress)
        MyBase.New(endpointConfigurationName, remoteAddress)
    End Sub
    
    Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
        MyBase.New(binding, remoteAddress)
    End Sub
    
    Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
        Return MyBase.Channel.Add(n1, n2)
    End Function
    
    Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
        Return MyBase.Channel.Subtract(n1, n2)
    End Function
    
    Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
        Return MyBase.Channel.Multiply(n1, n2)
    End Function
    
    Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
        Return MyBase.Channel.Divide(n1, n2)
    End Function
End Class
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.1366
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------



[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://Microsoft.ServiceModel.Samples", ConfigurationName="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);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface ICalculatorChannel : ICalculator, System.ServiceModel.IClientChannel
{
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class CalculatorClient : System.ServiceModel.ClientBase<ICalculator>, 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);
    }
}

これで Windows Communication Foundation (WCF) クライアントが作成されました。「方法 : 基本的な Windows Communication Foundation クライアントを構成する」に進み、クライアントを構成してください。トラブルシューティングの詳細については、「チュートリアル入門のトラブルシューティング」を参照してください。

参照

処理手順

入門サンプル
自己ホスト
方法 : 構成ファイルを使用してサービスのメタデータを公開する
方法 : Svcutil.exe を使用してメタデータ ドキュメントをダウンロードする

概念

ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe)