ServiceModel メタデータ ユーティリティ ツール (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 クライアントの概要」を参照してください。
注
SvcUtil.exe WCF クライアント クラスを生成すると、デバッガーが WCF クライアント クラスをステップ実行できないようにする DebuggerStepThroughAttribute がクライアント クラスに追加されます。
データ型の検索
生成されたコード内のデータ型を検索する最も基本的なメカニズムは、コントラクトで指定された型名を識別し、その型宣言のコードを検索することです。 たとえば、次のコントラクトは、 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
{
}