ProvideFault Method
Collapse the table of content
Expand the table of content

IErrorHandler.ProvideFault Method (Exception, MessageVersion, Message)


Enables the creation of a custom FaultException(Of TDetail) that is returned from an exception in the course of a service method.

Namespace:   System.ServiceModel.Dispatcher
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

Sub ProvideFault (
	error As Exception,
	version As MessageVersion,
	ByRef fault As Message


Type: System.Exception

The Exception object thrown in the course of the service operation.

Type: System.ServiceModel.Channels.MessageVersion

The SOAP version of the message.

Type: System.ServiceModel.Channels.Message

The System.ServiceModel.Channels.Message object that is returned to the client, or service, in the duplex case.

Implement the ProvideFault method to create a custom fault message that is returned to the client. The ProvideFault method will always be called for exceptions that leave operations, except when the WCF runtime recognizes the exception as especially fatal and rethrows the exception itself. When all ProvideFault implementations have been called, the fault message is sent back to the client (if fault is non-null). If the fault parameter is null the default fault is sent to the client.


The inbound message that caused the processing error is available during this method from the OperationContext.RequestContext property.


Because the ProvideFault 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 ProvideFault method being called on the operation thread.

The following code example demonstrates a service that implements IErrorHandler that returns only FaultException(Of 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
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
  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("") 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.

.NET Framework
Available since 3.0
Return to top
© 2015 Microsoft