Use a global exception handler to catch unhandled exceptions

J.D. Meier, Jason Taylor, Prashant Bansode, Carlos Farre, Madhu Sundararajan, Steve Gregersen.

Use structured exception handling and catch exception conditions. Doing this improves robustness and avoids leaving your application in an inconsistent state that may lead to information disclosure. It also helps protect your application from denial of service attacks.

You can handle the unhandled exceptions in a WCF service by subscribing to the Faulted event of a service host object. By subscribing to this event you can determine the cause of a failure, and then perform the necessary actions to abort or restart the service.

The following code snippet shows how to subscribe to the Faulted event.
// hosting a WCF service 
ServiceHost customerServiceHost;
customerServiceHost = new ServiceHost(…);
…
// Subscribe to the Faulted event of the customerServiceHost object
customerServiceHost.Faulted += new EventHandler(faultHandler);
…
// FaultHandler method - invoked when customerServiceHost enters the Faulted state
void faultHandler(object sender, EventArgs e)
{     // log the reasons for the fault  ..
}

Last edited Apr 16, 2008 at 11:32 PM by prashantbansode, version 1

Comments

snakefoot Dec 7, 2009 at 9:23 AM 
Another way to get the exception details of what caused the fault. Credits http://www.haveyougotwoods.com/archive/2009/06/24/creating-a-global-error-handler-in-wcf.aspx

public class ErrorHandler : IErrorHandler
{
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{

}

public bool HandleError(Exception error)
{
Console.WriteLine("exception");
return false;
}
}

public class ErrorServiceBehavior : IServiceBehavior
{
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{

}

public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{

}

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
ErrorHandler handler = new ErrorHandler();
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(handler);
}
}
}


servicehost.Description.Behaviors.Add(new ErrorServiceBehavior());

freecarver Jan 29, 2009 at 6:36 AM 
I have the same problem to get the reason for the fault. The EventArgs are sent as EventArgs.Empty.
The only way Icould get the last occured exception was through reflection code like that:

/// <summary>
/// FaultHandler method - invoked when customerServiceHost enters the Faulted state
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void FaultHandler(object sender, EventArgs e)
{
// log the reasons for the fault ..
this.EventLog.WriteEntry(this.GetCurrentException((CommunicationObject)sender).Message);
}

/// <summary>
/// Gets the last occured exception from the exception queue hosted in the <see cref="CommunicationObject"/>.
/// </summary>
/// <param name="com">The <see cref="CommunicationObject"/>.</param>
/// <returns>The last occured Exception of the <see cref="CommunicationObject"/>.</returns>
private Exception GetCurrentException(CommunicationObject com)
{
var exceptionQueue =
com.GetType().GetField("exceptionQueue", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(com);
return
(Exception)
exceptionQueue.GetType().GetMethod("GetException", BindingFlags.Instance | BindingFlags.Public).Invoke(
exceptionQueue, null);
}

No idea if that could work.

tirthankar8 Jan 11, 2009 at 2:42 PM 
The example above shows that we bind it during hosting a service as Windows Service. What is required to be done when the WCF is hosted on IIS?

EvilShrike Apr 21, 2008 at 9:35 AM 
How to get fault reason in Fauled-event handler (faultHandler) ? We just have EventArgs instance...