生成されたクライアント コードの理解

ServiceModel Metadata Utility Tool (Svcutil.exe) は、クライアント アプリケーションの構築時に使用するクライアント コードとクライアント アプリケーション構成ファイルを生成します。 このトピックでは、標準サービス コントラクトのシナリオ向けに生成されたコード例について説明します。 生成されたコードでクライアント アプリケーションを構築する方法の詳細については、「WCF クライアントの概要」を参照してください。

概要

Visual Studio を使用してプロジェクトの Windows Communication Foundation (WCF) クライアント型を生成する場合、通常、生成されたクライアント コードを調べる必要はありません。 同じサービスを実行する開発環境を使用していない場合は、Svcutil.exe のようなツールを使用してクライアント コードを生成し、そのコードでクライアント アプリケーションを開発します。

Svcutil.exe には、生成された型情報を変更する多くのオプションがあるため、ここですべてのシナリオを説明することはできません。 ここでは、生成されたコードの検索に関する標準的な作業の例を紹介します。

  • サービス コントラクト インターフェイスの識別

  • WCF クライアント クラスの識別

  • データ型の識別

  • 双方向サービスのコールバック コントラクトの識別

  • ヘルパーのサービス コントラクトのチャネル インターフェイスの識別

サービス コントラクト インターフェイスの検索

サービス コントラクトをモデル化するインターフェイスを検索するには、 System.ServiceModel.ServiceContractAttribute 属性でマークされたインターフェイスを検索します。 多くの場合、属性自体に設定された他の属性や明示的なプロパティが存在するため、この属性をすばやく読み取って見つけることは簡単ではありません。 サービス コントラクト インターフェイスとクライアント コントラクト インターフェイスは 2 つの別の種類だという点に注意してください。 次のコード例は、元のサービス コントラクトを示しています。

[ServiceContractAttribute(
  Namespace = "http://microsoft.wcf.documentation"
)]
public interface ISampleService
{
  [OperationContractAttribute]
  [FaultContractAttribute(typeof(microsoft.wcf.documentation.SampleFault))]
  string SampleMethod(string msg);
}

次のコード例は、Svcutil.exe で生成された同じサービス コントラクトを示しています。

[System.ServiceModel.ServiceContractAttribute(
  Namespace = "http://microsoft.wcf.documentation"
)]
public interface ISampleService
{
    [System.ServiceModel.OperationContractAttribute(
      Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethod",
      ReplyAction = "http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
    )]
    [System.ServiceModel.FaultContractAttribute(
      typeof(microsoft.wcf.documentation.SampleFault),
      Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethodSampleFaultFault"
    )]
    string SampleMethod(string msg);
}

生成されたサービス コントラクト インターフェイスと System.ServiceModel.ChannelFactory クラスを使用して、サービス操作の呼び出しに使用する WCF チャネル オブジェクトを作成できます。 詳細については、「方法: ChannelFactory を使用する」を参照してください。

WCF クライアント クラスの検索

使用するサービス コントラクトを実装する WCF クライアント クラスを検索するには、System.ServiceModel.ClientBase<TChannel> の拡張を検索します。ここで、型パラメーターは、以前に検索され、自身を拡張するサービス コントラクト インターフェイスです。 次のコード例は、 ClientBase<TChannel> 型の ISampleServiceクラスを示しています。

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class SampleServiceClient : System.ServiceModel.ClientBase<ISampleService>, ISampleService
{

    public SampleServiceClient()
    {
    }

    public SampleServiceClient(string endpointConfigurationName)
        :
            base(endpointConfigurationName)
    {
    }

    public SampleServiceClient(string endpointConfigurationName, string remoteAddress)
        :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public SampleServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress)
        :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public SampleServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress)
        :
            base(binding, remoteAddress)
    {
    }
    public string SampleMethod(string msg)
    {
        return base.Channel.SampleMethod(msg);
    }
}

この WCF クライアント クラスを使用するには、新しいインスタンスを作成し、このクラスに実装されているメソッドを呼び出します。 これらのメソッドは、対応しているサービス操作を呼び出し、やり取りを行うように構成されています。 詳細については、「WCF クライアントの概要」を参照してください。

Note

SvcUtil.exe で WCF クライアント クラスが生成されるとき、 DebuggerStepThroughAttribute がクライアント クラスに追加されるため、デバッガーで WCF クライアント クラスをステップ実行できなくなります。

データ型の検索

生成されたコード内でデータ型を検索する最も基本的な方法は、コントラクトに指定された型名を識別し、その型宣言のコードを検索することです。 たとえば、次のコントラクトには、 SampleMethod が型 microsoft.wcf.documentation.SampleFaultの SOAP エラーを返せることが指定されています。

[System.ServiceModel.OperationContractAttribute(
  Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethod",
  ReplyAction = "http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
)]
[System.ServiceModel.FaultContractAttribute(
  typeof(microsoft.wcf.documentation.SampleFault),
  Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethodSampleFaultFault"
)]
string SampleMethod(string msg);

SampleFault を検索すると、次の型宣言が見つかります。

[assembly: System.Runtime.Serialization.ContractNamespaceAttribute(
  "http://microsoft.wcf.documentation",
  ClrNamespace = "microsoft.wcf.documentation"
)]
namespace microsoft.wcf.documentation
{
    using System.Runtime.Serialization;

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class SampleFault : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
        private string FaultMessageField;

        public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        {
            get
            {
                return this.extensionDataField;
            }
            set
            {
                this.extensionDataField = value;
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public string FaultMessage
        {
            get
            {
                return this.FaultMessageField;
            }
            set
            {
                this.FaultMessageField = value;
            }
        }
    }
}

この場合、このデータ型は、クライアントの特定の例外 ( FaultException<TDetail> ) によりスローされる詳細な型です。詳細な型のパラメーターは、 microsoft.wcf.documentation.SampleFaultです。 データ型の詳細については、「サービス コントラクトでのデータ転送の指定」を参照してください。 クライアントの例外処理の詳細については、「エラーの送受信」を参照してください。

双方向サービスのコールバック コントラクトの検索

コントラクト インターフェイスにより ServiceContractAttribute.CallbackContract プロパティの値が指定されているサービス コントラクトを検索すると、そのコントラクトには双方向コントラクトが指定されていることがわかります。 二重のコントラクトを使用した場合、クライアント アプリケーションは、コールバック コントラクトを実装するコールバック クラスを作成し、そのクラスのインスタンスを、サービスとの通信に使用する System.ServiceModel.DuplexClientBase<TChannel> または System.ServiceModel.DuplexChannelFactory<TChannel> に渡す必要があります。 二重クライアントの詳細については、「方法: 双方向コントラクトを使用してサービスにアクセスする」を参照してください。

次のコントラクトは、型 SampleDuplexHelloCallbackのコールバック コントラクトを指定しています。

[System.ServiceModel.ServiceContractAttribute(
  Namespace="http://microsoft.wcf.documentation",
  ConfigurationName="SampleDuplexHello",
  CallbackContract=typeof(SampleDuplexHelloCallback),
  SessionMode=System.ServiceModel.SessionMode.Required
)]
public interface SampleDuplexHello
{
  [System.ServiceModel.OperationContractAttribute(
      IsOneWay=true,
      Action="http://microsoft.wcf.documentation/SampleDuplexHello/Hello"
    )]
    void Hello(string greeting);
  }
    <System.ServiceModel.OperationContractAttribute(IsOneWay:=True, _
        Action:="http://microsoft.wcf.documentation/SampleDuplexHello/Hello")> _
    Sub Hello(ByVal greeting As String)
End Interface 'SampleDuplexHello

このコールバック コントラクトを検索すると、クライアント アプリケーションが実装する必要のある次のインターフェイスが見つかります。

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface SampleDuplexHelloCallback
{
  [System.ServiceModel.OperationContractAttribute(
      IsOneWay=true,
      Action="http://microsoft.wcf.documentation/SampleDuplexHello/Reply"
    )]
    void Reply(string responseToGreeting);
  }
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")>  _
Public Interface SampleDuplexHelloCallback
    <System.ServiceModel.OperationContractAttribute( _
        IsOneWay:=True, _
        Action:="http://microsoft.wcf.documentation/SampleDuplexHello/Reply")> _
    Sub Reply(ByVal responseToGreeting As String)
End Interface 'SampleDuplexHelloCallback

サービス コントラクト チャネル インターフェイスの検索

サービス コントラクト インターフェイスで ChannelFactory クラスを使用する場合、明示的にチャネルを開いたり、閉じたり、中止したりするには、 System.ServiceModel.IClientChannel インターフェイスにキャストする必要があります。 処理を容易にするために、Svcutil.exe ツールでは、サービス コントラクト インターフェイスと IClientChannel の両方を実装するヘルパー インターフェイスも生成されます。これにより、キャストを行わずにクライアント チャネル インフラストラクチャとのやりとりを実現できます。 上記のサービス コントラクトを実装するヘルパー クライアント チャネルの定義を、次のコード例に示します。

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

関連項目