エラー関連処理の実行を可能にし、後続の
HandleError 実装が呼び出されるかどうかを示す値を返します。
名前空間: System.ServiceModel.Dispatcher
アセンブリ: System.ServiceModel (system.servicemodel.dll 内)
Function HandleError ( _
error As Exception _
) As Boolean
Dim instance As IErrorHandler
Dim error As Exception
Dim returnValue As Boolean
returnValue = instance.HandleError(error)
bool HandleError (
Exception error
)
bool HandleError (
Exception^ error
)
boolean HandleError (
Exception error
)
function HandleError (
error : Exception
) : boolean
パラメータ
- error
処理中にスローされた例外。
戻り値
後続の IErrorHandler の実装を呼び出さない場合は true、それ以外の場合は false。既定値は false です。
HandleError メソッドを使用して、エラー ログの記録、システム通知、アプリケーションのシャットダウンなどのエラー関連動作を実装します。
エラー ハンドラが HandleError メソッドから true を返さない場合は、例外は未処理と見なされ、セッション チャネルで通信するときに System.ServiceModel.InstanceContext とチャネルが中断するか、または ServiceBehaviorAttribute.InstanceContextMode プロパティが InstanceContextMode.PerSession に設定されます。
error パラメータは、null 参照 (Visual Basic では Nothing) になることはなく、スローされた例外オブジェクトを格納します。
次のコード例では、サービス メソッドがマネージ例外をスローしたときに、GreetingFault 型の FaultException だけを返す IErrorHandler を実装するサービスを示します。
#region IErrorHandler Members
public bool HandleError(Exception error)
{
Console.WriteLine("HandleError called.");
// Returning true indicates you performed your behavior.
return true;
}
// This is a trivial implementation that converts Exception to FaultException<GreetingFault>.
public void ProvideFault(
Exception error,
MessageVersion ver,
ref Message msg
)
{
Console.WriteLine("ProvideFault called. Converting Exception to GreetingFault....");
FaultException<GreetingFault> fe
= new FaultException<GreetingFault>(new GreetingFault(error.Message));
MessageFault fault = fe.CreateMessageFault();
msg = Message.CreateMessage(
ver,
fault,
"http://microsoft.wcf.documentation/ISampleService/SampleMethodGreetingFaultFault"
);
}
#endregion
次のコード例では、サービス動作を使用して、IErrorHandler の実装を ErrorHandlers プロパティに追加する方法を示します。
// 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
次のコード例では、アプリケーション構成ファイルを使用してサービス動作を読み込むようにサービスを構成する方法を示します。構成ファイル内のサービス動作を公開する方法の詳細については、「IServiceBehavior」を参照してください。
<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