views:

464

answers:

1

Hello,

I'm hosting WCF services in Asp.net web page in ASP.NET Compatibility Mode (AspNetCompatibilityRequirementsMode.Allowed). I've written simple HttpModule:

public class ExceptionInterceptor : IHttpModule
{
    public ExceptionInterceptor()
    {
    }

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.Error += new EventHandler(context_Error);
    }

    void context_Error(object sender, EventArgs e)
    {
        // do something
    }
}

web.config:

<httpModules>
        <add name="ExceptionInterceptor" type="HttpModules.ExceptionInterceptor, HttpModules"/>
</httpModules>

My question is, why after occurence of unhandled exception in service, the code do not enter in context_Error(object sender, EventArgs e) function in my module.

What's more, the code do not even enter the Application_Error(object sender, EventArgs e) in Globals.asax. Can someone explain that to me ?

What is the best option for global exception handling in WCF services ?

Regards

+1  A: 

WCF is not ASP.NET - it might be able to use some of ASP.NET's infrastructure, but it's not ASP.NET per se.

In order to handle errors in a WCF service globally, you need to implement the IErrorHandler interface on your service - or plug in a WCF behavior that does this for you.

Check out the MSDN documentation on IErrorHandler - it's quite a simple interface, really. The HandleError method is typically used to log the error on the server to keep track of what's going on, while the ProvideFault method is used to turn the .NET exception on the server into an interoperable SOAP fault to send that back to the calling client (which might be a non-.NET client that can't really deal with a .NET-specific exception).

Rory Primrose has a great blog post about how to package up the IErrorHandler into a WCF service behavior which you can easily add to an existing service just in config - pretty close to magic :-) Also, check out another great post on the topic by Steve Barbour.

marc_s
Thanks for the response. I've actually yesterday implemented the same solution from msdn. Still I don't why the compatibility mode is not workink for HttpModule but that's actually not the case for me now.
Jarek Waliszko
@Jarek: the HttpModule doesn't work, because a WCF service is **not** an ASP.NET web application. It handles its errors differently.
marc_s