IErrorHandler.ProvideFault Method
Enables the creation of a custom FaultException<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)
Parameters
- error
- Type: System.Exception
The Exception object thrown in the course of the service operation.
- version
- Type: System.ServiceModel.Channels.MessageVersion
The SOAP version of the message.
- fault
- 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.
Note |
|---|
The inbound message that caused the processing error is available during this method from the OperationContext.RequestContext property. |
Note |
|---|
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<TDetail> of type GreetingFault when a service method throws a managed exception.
#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
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 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
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 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note