多くの場合、同じ人物がサービスを設計して実装するわけではありません。 相互運用アプリケーションが重要な環境では、コントラクトは Web サービス記述言語 (WSDL) で設計または記述でき、開発者は提供されたコントラクトに準拠するサービスを実装する必要があります。 また、既存のサービスを Windows Communication Foundation (WCF) に移行しても、ワイヤ形式を保持することもできます。 さらに、双方向コントラクトでは、呼び出し元もコールバック コントラクトを実装する必要があります。
このような場合は、 ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) (または同等のツール) を使用して、コントラクトの要件を満たすために実装できるマネージド言語でサービス コントラクト インターフェイスを生成する必要があります。 通常、 ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) は、チャネル ファクトリまたは WCF クライアントの種類だけでなく、適切なバインドとアドレスを設定するクライアント構成ファイルで使用されるサービス コントラクトを取得するために使用されます。 生成された構成ファイルを使用するには、それをサービス構成ファイルに変更する必要があります。 サービス コントラクトの変更が必要な場合もあります。
データを取得し、準拠しているサービスを実装するには
メタデータ ファイルまたはメタデータ エンドポイントに対して ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を使用して、コード ファイルを生成します。
System.ServiceModel.ServiceContractAttribute属性でマークされている対象のインターフェイス (複数ある場合) を含む出力コード ファイルの部分を検索します。 次のコード例は、 ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) によって生成される 2 つのインターフェイスを示しています。 最初の (
ISampleService
) は、準拠しているサービスを作成するために実装するサービス コントラクト インターフェイスです。 2 つ目の (ISampleServiceChannel
) は、サービス コントラクト インターフェイスと System.ServiceModel.IClientChannel の両方を拡張するクライアント用のヘルパー インターフェイスであり、クライアント アプリケーションで使用します。[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [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.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public interface ISampleServiceChannel : ISampleService, System.ServiceModel.IClientChannel { }
WSDL がすべての操作に対して応答アクションを指定しない場合、生成される操作コントラクトには、ワイルドカード文字 (*) に ReplyAction プロパティが設定されている可能性があります。 このプロパティ設定を削除します。 それ以外の場合、サービス コントラクトメタデータを実装する場合、それらの操作に対してメタデータをエクスポートすることはできません。
クラスにインターフェイスを実装し、サービスをホストします。 例については、「 方法: サービス コントラクトを実装する」を参照するか、「例」セクションの下の簡単な実装を参照してください。
ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) によって生成されるクライアント構成ファイルで、<client> 構成セクションを <services> 構成セクションに変更します。 (生成されたクライアント アプリケーション構成ファイルの例については、次の「例」セクションを参照してください)。
<services>構成セクション内で、サービス実装の<services>構成セクションに
name
属性を作成します。サービス
name
属性を、サービス実装の構成名に設定します。実装されたサービス コントラクトを使用するエンドポイント構成要素をサービス構成セクションに追加します。
例
次のコード例は、メタデータ ファイルに対して ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を実行して生成されるコード ファイルの大部分を示しています。
このコードには、次の項目が示されています。
実装時にコントラクト要件 (
ISampleService
) に準拠するサービス コントラクト インターフェイス。サービス コントラクト インターフェイスと System.ServiceModel.IClientChannel の両方を拡張し、クライアント アプリケーション (
ISampleServiceChannel
) で使用するクライアント用のヘルパー インターフェイス。System.ServiceModel.ClientBase<TChannel>を拡張し、クライアント アプリケーション (
SampleServiceClient
) で使用するためのヘルパー クラス。サービスから生成された構成ファイル。
単純な
ISampleService
サービスの実装。クライアント側の構成ファイルからサービス側のバージョンへの変換。
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.42
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
[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;
}
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[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.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface ISampleServiceChannel : ISampleService, System.ServiceModel.IClientChannel
{
}
[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);
}
}
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISampleService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8080/SampleService" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ISampleService" contract="ISampleService"
name="BasicHttpBinding_ISampleService" />
</client>
</system.serviceModel>
</configuration>
// Implement the service. This is a very simple service.
class SampleService : ISampleService
{
public string SampleMethod(string msg)
{
Console.WriteLine($"The caller said: \"{msg}\"");
return "The service greets you: " + msg;
}
}
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISampleService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service
name="Microsoft.WCF.Documentation.SampleService">
<endpoint address="http://localhost:8080/SampleService" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ISampleService" contract="Microsoft.WCF.Documentation.ISampleService"
/>
</service>
</services>
</system.serviceModel>
</configuration>