Problem with enterprise library error handling and multithreading

Topics: Enterprise Library Core, Exception Handling Application Block
Mar 15, 2013 at 7:50 PM

I use Exception handling application block of enterprise library. In my handlers I use a threadstatic field (from specific class in my application).

Lets call this field MyContext.ID for the exsample.

My applicaion running with several threads and I use the ExceptionManager.HandleException(...) in each thread.

Sometimes the handlers gets the wrong MyContext.ID (from other thread).

What sould I do?

I already tried to use enterpriselibrarycontainer.CreateDefaultContainer(...).getinstance intead of enterpriselibrarycontainer.current.getinstance in each thread, I also tried to use lock in the handlers.
Mar 16, 2013 at 2:12 AM
As far as I know the Exception Handling Block should be thread safe. Exception Policies (and the contained IExceptionHandlers) will only be injected into the ExceptionManagerImpl once so if you have created custom IExceptionHandlers then those must also be thread safe.

Could you attach your context information to the exception's Data IDictionary? This way the handler could retrieve the pertinent context information from an instance variable and avoid any potential threading issues.

Randy Levy
Enterprise Library support engineer
Support How-to
Mar 16, 2013 at 3:17 PM
Thanks for quick reply.
But I also get a MemoryStream from the MyContext class in my handler (it could get up to several MB) and I don't think it's a good idea to attach it to the exception.
Also I know that there are some exceptions that thier Data IDictionary is null and you can't set it :(
Mar 19, 2013 at 4:42 AM
Are you able to reproduce the behavior? Perhaps a sample project would help track down the issue?

Randy Levy
Enterprise Library support engineer
Support How-to