How to use Exception Handling Application Block in a c# class library project

Topics: Exception Handling Application Block
Mar 30, 2011 at 1:02 PM

I have a class library project build in vs 2010 with c#. The production of the project is a dll (a set of tools) that can be loaded in another stand along application. When I am developing, I need to set "start external program" to the stand along exe to debug this dll. How can I set the configuration for this class library to use Exception Handling Application Block since there is no app.config?


Mar 31, 2011 at 12:35 AM

This blog will help you.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Mar 31, 2011 at 1:07 PM

Thanks for the link.

I'm trying to follow the section "Using Several Configuration Sources" and created a same exception.config as following:

        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
            <add name="Test Policy">
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                            <add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                replaceExceptionType="System.Exception, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />


In my code,

FileConfigurationSource exceptionsSource = new FileConfigurationSource(@"C:\Projects\ArcMapComponentTest\ArcMapComponentTest\exception.config"); ExceptionPolicyFactory exceptionFactory = new ExceptionPolicyFactory(exceptionsSource); ExceptionPolicyImpl exceptionPolicy = exceptionFactory.Create("Test Policy"); if (exceptionPolicy.HandleException(e)) throw;  

But the line ExceptionPolicyFactory exceptionFactory = new ExceptionPolicyFactory(exceptionsSource);


 throw this error;

Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

I followed this link "" and made sure the references are not pointing to local and also tried to switch "Enterprise Library 5 Assembly set" for the solution file to "Microsoft Signed" and didn' work.

Please help! Thanks

Apr 1, 2011 at 3:40 AM


Can you check if you are really referencing the right Assembly (with the right version and public key token) of Exception Handling? Also, can you try to re-reference the dll and see if it solves the problem?


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.

Apr 1, 2011 at 12:38 PM

I tried re-reference the dll to point to the installation folder "C:\Program Files (x86)\Microsoft Enterprise Library 5.0\Bin\" and still same error. By the way, the machine has Window 7. And my class library project is a ArcObject extension dll created using ArcGIS template targeted to .NET framework 4. 

Also, which  Enterprise Library config tool I should use (.NET4 or .NET 4(x86)) ?



Apr 4, 2011 at 3:26 AM

Did you run Visual Studio and the config tool if you're using the stand alone version with elevated permissions (Run as Administrator)?

As for your other question x86 means it's for 32-bit operating system.  Thus, I think you'll use the .NET 4 configuration .exe when you're running on a 64-bit os although you can also use it on 32-bit os.

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Apr 4, 2011 at 8:41 PM


I created a couple other test projects and tested with following line:

FileConfigurationSource exceptionsSource = new FileConfigurationSource(@"C:\Projects\ArcMapComponentTest\ArcMapComponentTest\exception4.config");
                ExceptionPolicyFactory exceptionFactory = new ExceptionPolicyFactory(exceptionsSource);
                ExceptionPolicyImpl exceptionPolicy = exceptionFactory.Create("Test Policy");
                if (exceptionPolicy.HandleException(e))

I force the code to created a "FileNotFoundException". These is the result:

1. Create a window form project and place above code in a button click event and worked. Then created a class library in the same solution and move about code to the class project and worked.

2. Create a different solution with just a class library with above code and use a separate win form executable to call the class library, and this does not work.

Does this mean that the separate configuration can not work in a stand along class library? If no, could you provide an example?



Apr 5, 2011 at 2:44 AM

Class libraries don't own configuration files.  If you're entlib code is on a class library, your entlib configuration should be in the executable project calling your code in the class library.  It's possible though to have your code read from a specific configuration file by making use of a FileConfigurationSource.  Check out this blog.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Apr 5, 2011 at 11:55 AM

I've been trying to use FileConfigurationSource (like the code snipet in my post). But what I need to confirm is that reading the specific config file still has to happen in the executable project calling the class library, correct? Because the class library project I'm going to work on will have not control of the calling executable, i.e. there no way I can feed a config file into the executable. The classe library will be just a tool (add on) for the executable.


Apr 6, 2011 at 12:58 AM

Yes, the class library will still read the configuration file of the host project.  Sorry, I misunderstood your problem. 

So to make sure I understand it, your code which uses the FileConfigurationSource is in a class defined in a class library.  It previously works but when you changed the host project, it throws the exception saying it can't load or find the ExceptionHandling assembly, right?  If this is the case, did you check that the said assembly is referenced in the new host project?


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Apr 6, 2011 at 11:52 AM

Thanks, Sarah!

The only way I can make it work is that I created a test win form project and then add a class library project (with exception handling code) in the same solution.

But the class library project I'm working on is a stand alone project which is a customized tool for ArcMap (an enterprise GIS software). And I set "Start external program" in project property to the executable (ArcMap.exe) so I can load my tool (class library) to test/use. So I have no control to the host program (ArcMap.exe). Is there a solution for this situation?


Apr 7, 2011 at 12:33 AM
Edited Apr 7, 2011 at 9:18 AM

I'm actually not familiar with ArcMap, but in .NET applications, it needs to have a reference to that assembly or at least the assembly must be present in its output folder so tha the CLR will be able to load it at runtime.  Maybe you need to dig how to make this work in ArcMap.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Apr 7, 2011 at 1:28 PM

Hi Sarah,

I created a very simple class library project in VS 2010 "EntLibComTest" and defined a config file to handle exception. Then I created a separate Win Form project (EntLibComTestForm4) in a different solution which calls this class library.

Could you help to confirm is there a way if this would work?

Your help is greatly appreciated!

This e-mail and any attachments contain URS Corporation confidential information that may be proprietary or privileged. If you receive this message in error or are not the intended recipient, you should not retain, distribute, disclose or use any of this information and you should destroy the e-mail and any attachments or copies.

Apr 8, 2011 at 12:55 AM

Yes it would work.  You'll reference the output assembly of the class library.  Define the exceptionHandling config in your EntLibComTestForm4 and make sure you reference the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling assembly.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Jun 14, 2012 at 6:31 AM

Hi Julie

Have you ever solved this problem? I am having exact same issue with ArcMap calling entlib.

Any help is highly appreciated