Modify postHandlingAction at runtime

Topics: Exception Handling Application Block, Policy Injection Application Block
Mar 28, 2013 at 9:04 AM
Is there any way to modify “postHandlingAction” attribute at runtime?

We have a requirement to catch all exception using custom exception handler and manage the exception based on some business logic.

Custom exception handler,
    public class CustomProvider2 : IExceptionHandler
        public Exception HandleException(Exception exception, Guid handlingInstanceId)
            switch (exceptionName)
                case "FaultException":
                    //if Exception is  FaultException then set postHandlingAction="ThrowNewException"
                case "CommunicationException":
                    //if Exception is  CommunicationException then set postHandlingAction="NotifyRethrow"
                    // postHandlingAction="None"

            return exception;
And it’s configuration like the following,
      <add name="Policy">
          <add name="AllException" type="System.SystemException, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089"
              <add type="ExceptionHandling.CustomProvider2, WebServiceTest, Version=, Culture=neutral, PublicKeyToken=null"
                name="CustomProvider" />
And implemented like the following,
        public void DoNothing()
                IUnityContainer unityContainer = new UnityContainer();
                var service2 = unityContainer.Resolve<IServices>("Impl1");
            catch (Exception ex)

If any exception occurred inside DoNothing() will invoke HandleException() method in custom handler. Based on some logic system will decide the exception will throw or handle.

Could someone explain how to change “postHandlingAction” property at runtime? Or any other method to achieve the same ?
Apr 1, 2013 at 2:23 AM
Edited Apr 5, 2013 at 2:46 PM
I don't think you can do what you want out of the box.

Based on the posted code it looks there is a one to one relationship between exception type and the postHandlingAction. If that is true then you could set up multiple exception types under a single policy. However, if the postHandlingAction does not (solely) depend on the exception type then I don't think there is much you can do. The reason is that the postHandlingAction is associated with the ExceptionPolicyEntry and not the Handler.

If you really wanted to implement this approach I think you would have to modify the source code to calculate the postHandlingAction based on the applicable business rules. This could involve changing the ExceptionPolicyEntry to do the check or perhaps putting the check in the ExceptionTypeData configuration class (depending on what information is required for the rule).

Randy Levy
Enterprise Library support engineer
Support How-to
Apr 5, 2013 at 2:14 PM
Randy, Thanks for your information.