Exception Handling Application Block Configuration Settings

Topics: Exception Handling Application Block
Jun 9, 2010 at 3:18 PM

Is it possible to define Exception Handling Application block setting i.e. exception policy, exception types and handlers in code behind instead of using configuration file?

Jun 10, 2010 at 6:48 AM

Yes, you can achieve this by utilizing EntLib 5 Fluent Configuration API. See the documentation for more details and sample http://msdn.microsoft.com/en-us/library/ff664363(v=PandP.50).aspx

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 10, 2010 at 5:40 PM

thanks Gino, this is what I need but the problem is I am using EntLib 4.1. Is there any similar API exist in EntLib 4.1 or any other way to achieve this?

 

Jun 11, 2010 at 2:15 AM

The fluent interface is not available in 4.1 so you need to do something like this:

ExceptionHandlingSettings exceptionHandlingSettings = new ExceptionHandlingSettings();
 //create the Exception Policy
ExceptionPolicyData exceptionPolicy = new ExceptionPolicyData("Exception Policy");
//create the Exception Type to handle
ExceptionTypeData exceptionType = new ExceptionTypeData("Exception", typeof(Exception).FullName, PostHandlingAction.ThrowNewException);
            
//create the exception handler to associate to the exception type
ReplaceHandlerData replaceHandler = new ReplaceHandlerData("Replace Handler", "replaced", typeof(ApplicationException).FullName);

//add the exception handler to the ExceptionTypeData
exceptionType.ExceptionHandlers.Add(replaceHandler);
//add the exception type to the exception policy
exceptionPolicy.ExceptionTypes.Add(exceptionType);
//add the exception policy to the ExceptionHandlingSettings
exceptionHandlingSettings.ExceptionPolicies.Add(exceptionPolicy);

DictionaryConfigurationSource configSource = new DictionaryConfigurationSource();
configSource.Add(ExceptionHandlingSettings.SectionName, exceptionHandlingSettings);

            
//sample code on how to utilize the DictionaryConfigurationSource           
try
{
       //code which might throw an exception
}
catch(Exception ex)
{
      ExceptionPolicyFactory factory = new ExceptionPolicyFactory(configSource);
      ExceptionPolicyImpl exceptionPolicyImpl = factory.Create("Exception Policy");
      bool rethrow = exceptionPolicyImpl.HandleException(ex);
      if (rethrow)
          throw;
}

Hope this helps.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com