ExceptionCallHandler via config

Topics: Exception Handling Application Block, General discussion, Policy Injection Application Block
Feb 28, 2013 at 2:35 PM
Edited Feb 28, 2013 at 2:36 PM
if you see below
line [ExceptionCallHandler("ExceptionPolicyName")]
i need to do this via config rather than in class..Please guide
public class Service : IService
{

   // wanted to achieve below line via config..please guide
    [ExceptionCallHandler("ExceptionPolicyName")]   
 public void Process()
    {
        throw new NotImplementedException();
    }

    [ExceptionCallHandler("ExceptionPolicyName2")]
    public void Process(int ivar)
    {
        throw new NotImplementedException();
    }

}
Feb 28, 2013 at 4:19 PM
Edited Feb 28, 2013 at 4:19 PM
also want to move this to config : [ExceptionCallHandler("ExceptionPolicyName")]
    [ExceptionCallHandler("ExceptionPolicyName")]
    public void Process()
    {
        throw new NotImplementedException();
    }
Mar 1, 2013 at 4:17 AM
Edited May 1, 2013 at 9:43 PM
The preferred approach is to use Unity instead of the legacy PolicyInjection facade.

So given that you have a Service defined like this:
    public interface IService
    {
        void Process();
        void Process(int var);
    }

    public class Service : IService
    {
        public void Process()
        {
            throw new NotImplementedException();
        }

        public void Process(int ivar)
        {
            throw new NotImplementedException();
        }
    }

You can then define in configuration an exception policy that will wrap the original NotImplementedException and throw an ApplicationException (I use ApplicationException for illustration -- it's not actually recommended to use ApplicationException).

The next step is to configure Unity to support interception, register the IService types to support interception and policy injection as well as create a matching rule to specify what needs to be intercepted and what call handlers to execute.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <exceptionHandling>
    <exceptionPolicies>
      <add name="Wrap and Throw">
        <exceptionTypes>
          <add name="All Exceptions" type="System.NotImplementedException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Wrap Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="Wrap the exception!" wrapExceptionType="System.ApplicationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
  
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">

    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>
    
    <container>
      <extension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common"/>
      <extension type="Interception"/>

      <register type="UnityCallHandlerConfig.IService, UnityCallHandlerConfig" mapTo="UnityCallHandlerConfig.Service, UnityCallHandlerConfig">
        <interceptor type="InterfaceInterceptor"/>
        <policyInjection/>
      </register>

      <interception>

        <policy name="Policy">
          <matchingRule name="Match" type="MemberNameMatchingRule">
            <constructor>
              <param name="nameToMatch" value="Process" />
            </constructor>
          </matchingRule>          
          <callHandler name="ExceptionCallHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <lifetime type="singleton"/>
            <constructor>
              <param name="exceptionPolicy" dependencyName="Wrap and Throw"/>
            </constructor>
          </callHandler>        
        </policy>
        
      </interception>

    </container>
 
  </unity>

</configuration>
That's the bulk of the work. The only thing left to do is create a Unity container, load the configuration, and invoke the method:
try
{
    var container = new UnityContainer().LoadConfiguration();

    var service = container.Resolve<IService>();
    service.Process();
}
catch (ApplicationException e)
{
    Console.WriteLine(e);
}
ApplicationException will be caught instead of the original NotImplementedException because of the call handler.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Mar 1, 2013 at 12:45 PM
thanks a lot