Wrap Exception in Web Api 2 not working

Topics: Exception Handling Application Block, General discussion
Jan 2, 2015 at 10:27 AM
Edited Jan 2, 2015 at 10:28 AM
Hi,

I'm trying to wrap exceptions by layer in a web api project, but exceptions are launched to client directly, not replaced.

Settings:
  • Framework 4.5.2
  • Unity 3.5.1404.0 (Nugget)
  • Enterprise Library Exception Handling App Block 6.0.1304 (Nugget)
Unity config file (Unity.config)

<?xml version="1.0" encoding="utf-8"?>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <!--Espacios de nombre y ensamblados-->
    <namespace name="Common.DataAccess" />
    <namespace name="Common.Business" />
    <namespace name="Common.DTO.Parser" />
    <namespace name="Common.WebApi.Controllers" />
    <assembly name="Common.DataAccess" />
    <assembly name="Common.IDataAccess" />
    <assembly name="Common.Business" />
    <assembly name="Common.IBusiness" />
    <assembly name="Common.DTO.Parser" />
    <assembly name="Common.DTO.IParser" />
    <assembly name="Common.WebApi" />

    <!--EnterpriseLibrary-->
    <assembly name="Microsoft.Practices.EnterpriseLibrary.PolicyInjection"/>
    <assembly name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
    <namespace name="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers"/>
    <namespace name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
    <namespace name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection"/>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
        
    <!--Alias-->
    <alias alias="perRequest" type="Microsoft.Practices.Unity.PerRequestLifetimeManager, Microsoft.Practices.Unity.Mvc" />
    
    <container>
      <!--Intercepciones Enterprise Library-->
      <extension type="Interception" />
      <interception>

        <policy name="DataAccessPolicy">
          <matchingRule name="DataAccessMatchRule" type="NamespaceMatchingRule">
            <constructor>
              <param name="namespaceName" value="Common.DataAccess.DBInteractions" />
            </constructor>
          </matchingRule>
          <callHandler name="ExceptionHandling" type="ExceptionCallHandler">
            <constructor>
              <param name="policyName" value="PolicyExceptionDataAccess"/>
            </constructor>
          </callHandler>
        </policy>

        <policy name="ServicesPolicy">
          <matchingRule name="ServicesMatchRule" type="NamespaceMatchingRule">
            <constructor>
              <param name="namespaceName" value="Common.Business" />
            </constructor>
          </matchingRule>
          <callHandler name="ExceptionHandling" type="ExceptionCallHandler">
            <constructor>
              <param name="policyName" value="PolicyExceptionBusiness"/>
            </constructor>
          </callHandler>
          <!--<callHandler name="ChangeLogHandler" type="ChangeLogCallHandler"/>-->
        </policy>

      </interception>

      <!--Contexto-->
      <register type="IDBFactory[CommonContext]" mapTo="DBFactory">
        <lifetime type="perRequest" />
      </register>
      <register type="IUnitOfWork" mapTo="UnitOfWork">
        <lifetime type="perRequest" />
        <constructor>
          <param name="databaseFactory"/>
        </constructor>
      </register>
      
      <!--DAO-->
      <register type="IAplicacionDAO" mapTo="AplicacionDAO">
        <lifetime type="perRequest" />
        <constructor>
          <param name="databaseFactory"/>
        </constructor>
      </register>
      <register type="IEstafadorDAO" mapTo="EstafadorDAO">
        <lifetime type="perRequest" />
        <constructor>
          <param name="databaseFactory"/>
        </constructor>
      </register>
      <register type="IOrigenIrregularidadDAO" mapTo="OrigenIrregularidadDAO">
        <lifetime type="perRequest" />
        <constructor>
          <param name="databaseFactory"/>
        </constructor>
      </register>
      <register type="IPerfilRiesgoDAO" mapTo="PerfilRiesgoDAO">
        <lifetime type="perRequest" />
        <constructor>
          <param name="databaseFactory"/>
        </constructor>
      </register>
      <register type="ISuplantacionDAO" mapTo="SuplantacionDAO">
        <lifetime type="perRequest" />
        <constructor>
          <param name="databaseFactory"/>
        </constructor>
      </register>
      <!--Business-->
      <register type="IAplicacionService" mapTo="AplicacionService">
        <lifetime type="perRequest" />
        <constructor>
          <param name="repository"/>
          <param name="unitOfWork"/>
        </constructor>
      </register>
      <register type="IEstafadorService" mapTo="EstafadorService">
        <lifetime type="perRequest" />
        <constructor>
          <param name="repository"/>
          <param name="unitOfWork"/>
        </constructor>
      </register>
      <register type="IOrigenIrregularidadService" mapTo="OrigenIrregularidadService">
        <lifetime type="perRequest" />
        <constructor>
          <param name="repository"/>
          <param name="unitOfWork"/>
        </constructor>
      </register>
      <register type="IPerfilRiesgoService" mapTo="PerfilRiesgoService">
        <lifetime type="perRequest" />
        <constructor>
          <param name="repository"/>
          <param name="unitOfWork"/>
        </constructor>
      </register>
      <register type="ISuplantacionService" mapTo="SuplantacionService">
        <lifetime type="perRequest" />
        <constructor>
          <param name="repository"/>
          <param name="unitOfWork"/>
        </constructor>
      </register>
      <!--Parser-->
      <register type="IEstafadorParser" mapTo="EstafadorParser">
        <lifetime type="perRequest" />
        <constructor>
          <param name="suplantacionService"/>
          <param name="origenIrregularidadService"/>
          <param name="perfilRiesgoService"/>
        </constructor>
      </register>
      <!--Controllers-->
      <register type="EstafadorController" name="estafadorController">
        <lifetime type="perRequest" />
        <constructor>
          <param name="unitOfWork"/>
          <param name="estafadorService"/>
          <param name="estafadorParser"/>
        </constructor>
      </register>

    </container>
    
  </unity>
Policies config (ExceptionHandling.config)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <exceptionHandling>
    <exceptionPolicies>

      <add name="PolicyExceptionDataAccess">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
              postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Wrap DataAccess" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                  wrapExceptionType="Utils.Exceptions.DataAccessException, Utils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>

      <add name="PolicyExceptionBusiness">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
              postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Wrap Business" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WrapHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                   wrapExceptionType="Utils.Exceptions.ServiceException, Utils, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
</configuration>
Enterprise activator class
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Common.WebApi.App_Start.EnterpriseLibraryActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(Common.WebApi.App_Start.EnterpriseLibraryActivator), "Shutdown")]

namespace Common.WebApi.App_Start
{
    public static class EnterpriseLibraryActivator
    {
        public static void Start()
        {
            // NOTE: Cargamos el control de excepciones
            //Configuracion de Manejo de Excepciones
            var exceptionConfig = new FileConfigurationSource("ExceptionHandling.config");
            var exceptionFactory = new ExceptionPolicyFactory(exceptionConfig);
            var exceptionManager = exceptionFactory.CreateManager();
            ExceptionPolicy.SetExceptionManager(exceptionManager);
        }

        public static void Shutdown()
        {
            ExceptionPolicy.Reset();
        }
    }
}
I can watch ExceptionPolicy is loaded correctly when a call is made to the service.

¿Could you help me? Thanks!