IErrorHandler.HandleError Method
Enables error-related processing and returns a value that indicates whether the dispatcher aborts the session and the instance context in certain cases.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
Parameters
- error
- Type: System.Exception
The exception thrown during processing.
Return Value
Type: System.Booleantrue if should not abort the session (if there is one) and instance context if the instance context is not Single; otherwise, false. The default is false.
Use the HandleError method to implement error-related behaviors such as error logging, system notifications, shutting down the application, and so on, and return a value that specifies whether the exception has been handled appropriately.
Note |
|---|
Because the HandleError method can be called from many different places there are no guarantees made about which thread the method is called on. Do not depend on HandleError method being called on the operation thread. |
All IErrorHandler implementations are called. By default (when the return value is false), if there is an exception, the dispatcher aborts any session and aborts the InstanceContext if the InstanceContextMode is anything other than Single. The exception is then considered unhandled and any state is considered corrupt.
Return true from HandleError(Exception) to prevent this default behavior. If any error handler returns true it instructs that it is safe to continue using state associated with the failed request.
If no error handler returns true from the HandleError method the exception is considered unhandled and the default response applies, potentially resulting in an aborted System.ServiceModel.InstanceContext and channel when communicating on a session channel or the ServiceBehaviorAttribute.InstanceContextMode property is not set to InstanceContextMode.Single.
The error parameter is never null and contains the exception object that was thrown.
The following code example demonstrates a service that implements IErrorHandler that returns only FaultException<TDetail> of type GreetingFault when a service method throws a managed exception.
#Region "IErrorHandler Members" Public Function HandleError(ByVal [error] As Exception) As Boolean Implements IErrorHandler.HandleError Console.WriteLine("HandleError called.") ' Returning true indicates you performed your behavior. Return True End Function ' This is a trivial implementation that converts Exception to FaultException<GreetingFault>. Public Sub ProvideFault(ByVal [error] As Exception, ByVal ver As MessageVersion, ByRef msg As Message) Implements IErrorHandler.ProvideFault Console.WriteLine("ProvideFault called. Converting Exception to GreetingFault....") Dim fe As New FaultException(Of GreetingFault)(New GreetingFault([error].Message)) Dim fault As MessageFault = fe.CreateMessageFault() msg = Message.CreateMessage(ver, fault, "http://microsoft.wcf.documentation/ISampleService/SampleMethodGreetingFaultFault") End Sub #End Region
The following code example shows how to use a service behavior to add the IErrorHandler implementation to the ErrorHandlers property.
' This behavior modifies no binding parameters. #Region "IServiceBehavior Members" Public Sub AddBindingParameters(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase, ByVal endpoints As System.Collections.ObjectModel.Collection(Of ServiceEndpoint), ByVal parameters As System.ServiceModel.Channels.BindingParameterCollection) Implements IServiceBehavior.AddBindingParameters Return End Sub ' This behavior is an IErrorHandler implementation and ' must be applied to each ChannelDispatcher. Public Sub ApplyDispatchBehavior(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase) Implements IServiceBehavior.ApplyDispatchBehavior Console.WriteLine("The EnforceGreetingFaultBehavior has been applied.") For Each chanDisp As ChannelDispatcher In serviceHostBase.ChannelDispatchers chanDisp.ErrorHandlers.Add(Me) Next chanDisp End Sub ' This behavior requires that the contract have a SOAP fault with a detail type of GreetingFault. Public Sub Validate(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase) Implements IServiceBehavior.Validate Console.WriteLine("Validate is called.") For Each se As ServiceEndpoint In description.Endpoints ' Must not examine any metadata endpoint. If se.Contract.Name.Equals("IMetadataExchange") AndAlso se.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex") Then Continue For End If For Each opDesc As OperationDescription In se.Contract.Operations If opDesc.Faults.Count = 0 Then Throw New InvalidOperationException(String.Format("EnforceGreetingFaultBehavior requires a " & "FaultContractAttribute(typeof(GreetingFault)) in each operation contract. " & "The ""{0}"" operation contains no FaultContractAttribute.", opDesc.Name)) End If Dim gfExists As Boolean = False For Each fault As FaultDescription In opDesc.Faults If fault.DetailType.Equals(GetType(GreetingFault)) Then gfExists = True Continue For End If Next fault If gfExists = False Then Throw New InvalidOperationException("EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract.") End If Next opDesc Next se End Sub #End Region
The following code example shows how to configure the service to load the service behavior using an application configuration file. For more details about how to expose a service behavior in a configuration file, see 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, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
</configuration>
<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, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
</configuration>
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note