Enterprise Library 6.0 : How to use Exception Manager instance in different project Layers?

Topics: Exception Handling Application Block
Mar 18, 2014 at 4:24 PM
Hi Friends,

I have created this class to configure the exception manager instance.
 public static class EnterpriseLibraryBootStrapper
{
    public static void Configure()
    {
        // Get Entlib config source (Current is in Web.EnterpriseLibrary.config)
        IConfigurationSource source = ConfigurationSourceFactory.Create();

        // Get factories from config
        var policyFactory = new ExceptionPolicyFactory(source);

        // Create Exception Manager Instance
        ExceptionManager exManager = policyFactory.CreateManager();


    }
}
I don't know, how to refer the exception manager instance?

Please find the code which was used in data access layer.
    ExceptionManager exceptionManager; 
   if (exceptionManager!= null)
  {
      exceptionManager.HandleException(ex, "Data Access Policy");
  }
Here i am getting compile time error because created exception manager instance is not referred. How to refer the created exception manager instance in different classes?

I am not using unity block. Kindly note.

I am new Enterprise Library 6.0. Please help.

Thanks,
Arun
Mar 19, 2014 at 3:03 AM
Edited Mar 19, 2014 at 3:58 AM
If you are not injecting dependencies (using a container such as Unity) then you will either need to use the static ExceptionPolicy facade or expose the ExceptionManager instance as a global singleton.

Here's how to use the facade:
    public static class EnterpriseLibraryBootStrapper
    {
        public static void Configure()
        {
            // Get Entlib config source (Current is in Web.EnterpriseLibrary.config)
            IConfigurationSource source = ConfigurationSourceFactory.Create();

            // Get factories from config
            var policyFactory = new ExceptionPolicyFactory(source);

            // Create Exception Manager Instance
            ExceptionManager exManager = policyFactory.CreateManager();

            ExceptionPolicy.SetExceptionManager(exManager);
        }
    }

// After configuring then can use the static facade
bool shouldRethrow = ExceptionPolicy.HandleException(ex, "Data Access Policy");

Alternatively you could create your own singleton (lazy loaded):
    public static class EnterpriseLibraryContainer
    {
        public static readonly Lazy<ExceptionManager> ExceptionManager = new Lazy<ExceptionManager>( () =>
        {
            // Get Entlib config source (Current is in Web.EnterpriseLibrary.config)
            IConfigurationSource source = ConfigurationSourceFactory.Create();

            // Get factories from config
            var policyFactory = new ExceptionPolicyFactory(source);

            // Create Exception Manager Instance
            ExceptionManager exManager = policyFactory.CreateManager();

            return exManager;
        });
    }

// Can use singleton any time
bool shouldRethrow = EnterpriseLibraryContainer.ExceptionManager.Value.HandleException(ex, "Data Access Policy");

If you wanted to eager load you could stick with the bootstrapper and assign the value to the singleton.

If your layers are separate processes/app domains then you would have to bootstrap each layer.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to