ServiceHostBase などの、サービス全体にわたるカスタム拡張機能を変更または挿入するための機構を提供します。
名前空間: System.ServiceModel.Description
アセンブリ: System.ServiceModel (system.servicemodel.dll 内)
Public Interface IServiceBehavior
Dim instance As IServiceBehavior
public interface IServiceBehavior
public interface class IServiceBehavior
public interface IServiceBehavior
public interface IServiceBehavior
サービス全体にわたる実行の一部の側面を、アプリケーション レベルで変更、確認、または拡張するには、IServiceBehavior を実装します。
-
ApplyDispatchBehavior メソッドを使用して、ランタイム プロパティの値を変更し、エラー ハンドラ、メッセージ インターセプタ、パラメータ インターセプタ、セキュリティ拡張などのカスタム拡張オブジェクトを挿入できるようにします。
-
Validate メソッドを使用して、Windows Communication Foundation (WCF) が実行対象のサービスを構築する前に説明を検査し、サービスを正常に実行できることを確認します。
-
AddBindingParameters メソッドを使用して、バインディング要素にサービスのカスタム情報を渡し、バインディング要素がサービスを正しくサポートできるようにします。
IServiceBehavior オブジェクトは、これらのメソッドをどれでも使用できますが、通常は、重要なメソッドは 1 つだけです。この場合、使用されないメソッドは、値なしで戻ることができます。
想定されているカスタマイズ タスクを実行するには、サービス ランタイムの構築の前に、IServiceBehavior オブジェクトを Behaviors プロパティに追加しておく必要があります。これには、次の 3 つの方法があります。
WCF のサービスの動作の例として、ServiceBehaviorAttribute 属性、System.ServiceModel.Description.ServiceThrottlingBehavior、System.ServiceModel.Description.ServiceDebugBehavior、System.ServiceModel.Description.ServiceMetadataBehavior 動作などがあります。
構成ファイルで指定されたサービスの動作を使用してカスタム エラー ハンドラをサービス アプリケーションに挿入する方法を次のコード例に示します。この例では、エラー ハンドラがすべての例外をキャッチし、それらを GreetingFault SOAP カスタム エラーに変換してからクライアントに返します。
次の IServiceBehavior 実装は、バインディング パラメータ オブジェクトは追加せず、カスタムの System.ServiceModel.Dispatcher.IErrorHandler オブジェクトを各 ChannelDispatcher.ErrorHandlers プロパティに追加します。さらに、サービスの動作が適用され、GreetingFault 型の System.ServiceModel.FaultContractAttribute を持つサービスの各操作を検証します。
// This behavior modifies no binding parameters.
#region IServiceBehavior Members
public void AddBindingParameters(
ServiceDescription description,
ServiceHostBase serviceHostBase,
System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
System.ServiceModel.Channels.BindingParameterCollection parameters
)
{
return;
}
// This behavior is an IErrorHandler implementation and
// must be applied to each ChannelDispatcher.
public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
{
Console.WriteLine("The EnforceGreetingFaultBehavior has been applied.");
foreach(ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
{
chanDisp.ErrorHandlers.Add(this);
}
}
// This behavior requires that the contract have a SOAP fault with a detail type of GreetingFault.
public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
{
Console.WriteLine("Validate is called.");
foreach (ServiceEndpoint se in description.Endpoints)
{
// Must not examine any metadata endpoint.
if (se.Contract.Name.Equals("IMetadataExchange")
&& se.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
continue;
foreach (OperationDescription opDesc in se.Contract.Operations)
{
if (opDesc.Faults.Count == 0)
throw new InvalidOperationException(String.Format(
"EnforceGreetingFaultBehavior requires a "
+ "FaultContractAttribute(typeof(GreetingFault)) in each operation contract. "
+ "The \"{0}\" operation contains no FaultContractAttribute.",
opDesc.Name)
);
bool gfExists = false;
foreach (FaultDescription fault in opDesc.Faults)
{
if (fault.DetailType.Equals(typeof(GreetingFault)))
{
gfExists = true;
continue;
}
}
if (gfExists == false)
{
throw new InvalidOperationException(
"EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract."
);
}
}
}
}
#endregion
この例では、動作クラスは、System.ServiceModel.Configuration.BehaviorExtensionElement も実装します。これで、次のコード例で示すように、サービス動作をアプリケーション構成ファイルで使用するよう挿入できます。
<configuration>
<system.serviceModel>
<services>
<service
name="Microsoft.WCF.Documentation.SampleService"
behaviorConfiguration="metaAndErrors">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/SampleService"/>
</baseAddresses>
</host>
<endpoint
address=""
binding="wsHttpBinding"
contract="Microsoft.WCF.Documentation.ISampleService"
/>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="metaAndErrors">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true"/>
<enforceGreetingFaults />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add
name="enforceGreetingFaults"
type="Microsoft.WCF.Documentation.EnforceGreetingFaultBehavior,HostApplication"
/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
</configuration>
Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。
.NET Framework
サポート対象 : 3.0