Configure Exception Call Handler interception using config file.

Topics: Building and extending application blocks, Enterprise Library Core, Exception Handling Application Block, General discussion, Policy Injection Application Block
Jan 18, 2012 at 5:59 AM

Hi,

I want to configure the ExceptionCallHandler interception using config file instead of attribute.

I write the sample application for exception call handler. But I am facing problem to configure ExceptionCallHandler. Since ExceptionCallHandler requires ExceptionPolicyImpl instance. I stuck here, How to pass ExceptionPolicyImpl object instace using container. My code is as below:--

Classes:-

  public interface ICalculator {

    Int32 Sum(Int32 x, Int32 y);
    //[NonNegativeCallHandler]
    Int32 Sub(Int32 x, Int32 y);
  }


  public class Calculator : MarshalByRefObject, ICalculator {

    public Int32 Sum(Int32 x, Int32 y) {
      if (x == 0 && y == 0)
        throw new Exception("Operands are zero.");
      return x + y;
    }

    public Int32 Sub(Int32 x, Int32 y) {
      if (x == 0 && y == 0)
        throw new InvalidOperationException("Operands are zero.");
      return x - y;
    }

    public void Hello() {
      Console.WriteLine("HEllow");
    }

  }


 

Exception Policy:-

<exceptionHandling>
    <exceptionPolicies>
      <add name="Policy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="Custom Exception" replaceExceptionType="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

Unity Block in config file:-

 

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="MatchingRuleUsingUnity"/>
    <namespace name="MatchingRuleUsingUnity, MatchingRuleUsingUnity"/>
    <alias alias="Calculator" type="MatchingRuleUsingUnity.Calculator, MatchingRuleUsingUnity"/>
    <alias alias="ICalculator" type="MatchingRuleUsingUnity.ICalculator, MatchingRuleUsingUnity"/>
    <alias alias="ScientificCalculator" type="MatchingRuleUsingUnity.ScienceCalculator.ScientificCalculator, MatchingRuleUsingUnity"/>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>

    <container>
      <extension type="Interception" />

      <register type="ICalculator" mapTo="Calculator">
        <interceptor type="TransparentProxyInterceptor"/>
        <interceptionBehavior type="PolicyInjectionBehavior" />
      </register>

      <register type="ScientificCalculator" >
        <interceptor type="TransparentProxyInterceptor"/>
        <interceptionBehavior type="PolicyInjectionBehavior" />
      </register>

      <interception>
        <policy name="non-negative">

  <matchingRule name="UIMemberNameRule" type="AssemblyMatchingRule">
            <constructor>
              <param name="assemblyName" value="MatchingRuleUsingUnity"/>
            </constructor>
          </matchingRule>


<callHandler name="ExceptionCallHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <constructor>
              <param name="exceptionPolicy"  value="Policy" />
            </constructor>
          </callHandler>

        </policy>
      </interception>

    </container>
  </unity>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <assembly name="MatchingRuleUsingUnity"/>
    <namespace name="MatchingRuleUsingUnity, MatchingRuleUsingUnity"/>
    <alias alias="Calculator" type="MatchingRuleUsingUnity.Calculator, MatchingRuleUsingUnity"/>
    <alias alias="ICalculator" type="MatchingRuleUsingUnity.ICalculator, MatchingRuleUsingUnity"/>
    <alias alias="ScientificCalculator" type="MatchingRuleUsingUnity.ScienceCalculator.ScientificCalculator, MatchingRuleUsingUnity"/>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>

    <container>
      <extension type="Interception" />

      <register type="ICalculator" mapTo="Calculator">
        <interceptor type="TransparentProxyInterceptor"/>
        <interceptionBehavior type="PolicyInjectionBehavior" />
      </register>

      <register type="ScientificCalculator" >
        <interceptor type="TransparentProxyInterceptor"/>
        <interceptionBehavior type="PolicyInjectionBehavior" />
      </register>

      <interception>
        <policy name="non-negative">

  <matchingRule name="UIMemberNameRule" type="AssemblyMatchingRule">
            <constructor>
              <param name="assemblyName" value="MatchingRuleUsingUnity"/>
            </constructor>
          </matchingRule>


<callHandler name="ExceptionCallHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <constructor>
              <param name="exceptionPolicy"  value="Policy" />
            </constructor>
          </callHandler>

        </policy>
      </interception>

    </container>
  </unity>


Since parameterr exceptionPolicy is of ExceptionPolicyImpl type. It can't resolving the type ExceptionPolicyImpl and throw an error
ii.e. TypeConverter cannot convert from System.String.

My problem is how to pass exception policy name into constructor?

Jan 18, 2012 at 6:43 AM

Instead of injecting a string value ("Policy") you need to specify the dependency name to resolve:

<callHandler name="ExceptionCallHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
  <constructor>
    <param name="exceptionPolicy"  dependencyName="Policy" />
  </constructor>
</callHandler>

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Jan 18, 2012 at 9:08 AM
Edited Jan 18, 2012 at 9:39 AM

Hi,

Thanks for the reply. Your suggestion work for me and error is resolved.

But still exception block doesn't call.

 

    public void Hello() {
      Console.WriteLine("HEllow");
    }

I throw a InvalidOperationException but at the calling point i receive the same InvalidOperationException instead of configured replaced exception "System.Exception" with my message "Custom Exception"

Jan 18, 2012 at 11:24 AM

The postHandlingAction is set to "NotifyRethrow".  Try changing it to "ThrowNewException" so that you get a new exception thrown.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Jan 20, 2012 at 4:42 AM

Hi Randylevy,

Thanks for your quick reply. You suggestion works for me.