ExceptionCallHandler with Unity 2.0

Jun 16, 2010 at 8:12 AM

Hi,

While trying to use exception call handler with unity 2.0, I am getting following exception

{"Resolution of the dependency failed, type = \"Northwind.IRepository.IUnitOfWork\", name = \"(none)\".\r\nException occurred while: Calling constructor Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior(Microsoft.Practices.Unity.InterceptionExtension.CurrentInterceptionRequest interceptionRequest, Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy[] policies, Microsoft.Practices.Unity.IUnityContainer container).\r\nException is: SynchronizationLockException - The read lock is being released without being held.\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n  Resolving Northwind.DataAccess.NorthwindContext,(none) (mapped from Northwind.IRepository.IUnitOfWork, (none))\r\n    Resolving Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior,(none)\r\n    Calling constructor Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior(Microsoft.Practices.Unity.InterceptionExtension.CurrentInterceptionRequest interceptionRequest, Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy[] policies, Microsoft.Practices.Unity.IUnityContainer container)\r\n"}

Inner exception says {"The read lock is being released without being held."}

I am using the following code:

            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<Interception>();
            container.AddNewExtension<EnterpriseLibraryCoreExtension>();

            container.RegisterType<IUnitOfWork, NorthwindContext>(
                new InterceptionBehavior<PolicyInjectionBehavior>(),
                new Interceptor<InterfaceInterceptor>());

            ExceptionPolicyImpl myPolicy
                = container.Resolve<ExceptionPolicyImpl>("RepositoryExceptionPolicy");

            container.Configure<Interception>()
                .AddPolicy("myPolicy")
                .AddMatchingRule<TypeMatchingRule>(new InjectionConstructor("Northwind.DataAccess.NorthwindContext",
                        true))
                    .AddCallHandler<Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler>
                    ("RepositoryExceptionPolicy", new ContainerControlledLifetimeManager());

            IUnitOfWork unitOfWork = container.Resolve<IUnitOfWork>();

IUnitOfWork unitOfWork = container.Resolve<IUnitOfWork>();

 

Please help.

Thanks,

Inderjeet

 

Jun 18, 2010 at 7:53 AM

I don't think "RepositoryExceptionPolicy" is the correct value to use in this line of code:

.AddCallHandler<Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler>
                    ("RepositoryExceptionPolicy", new ContainerControlledLifetimeManager());

The RepositoryExceptionPolicy is the name of your exception policy and maps to an ExceptionPolicyImpl.  However, in the code above you're referring to an ExceptionCallHandler. 

thus, your code should look like this:

.AddCallHandler<Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler>
                    (new ContainerControlledLifetimeManager(), new InjectionConstructor(myPolicy)); -> myPolicy is the instance of ExceptionPolicyImpl you resolved from the container.

There are actually various ways to configure policy using the API, you can refer to this topic from the documentation.

 

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

Jun 21, 2010 at 1:31 PM

Thanks, I was able to get it going using API. Currently, I am trying to achieve the same result through configuration but get an exception "TypeConverter cannot convert from System.String."

Given below is the configuration being used.

 

      <interception>
        <policy name="policy-queries">
          <matchingRule name="queries-rule1" type="TypeMatchingRule">
            <constructor>
              <param name="typeName" value="NorthwindRepository`1" />
            </constructor>
          </matchingRule>
          <callHandler type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Exception Handling Call Handler">
            <constructor>
              <param name="exceptionPolicy" value="RepositoryExceptionPolicy" />
            </constructor>
          </callHandler>          

        </policy>
      </interception>

Exception handling block has been configured as below

  <exceptionHandling>
    <exceptionPolicies>
      <add name="RepositoryExceptionPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                priority="0" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

Enterprise library extension has been added as below

<extension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common" />

Please suggest.

Thanks,

Inderjeet

 

 

Jun 21, 2010 at 6:59 PM

Change this:

<param name="exceptionPolicy" value="RepositoryExceptionPolicy" />

to:

<param name="exceptionPolicy" dependencyName="RepositoryExceptionPolicy" />

You don't want to stick the string in, you want to resolve through the container that particular named instance. This should get you what you want.

 

Jun 22, 2010 at 5:09 AM

Thanks, it works now.

Dec 29, 2011 at 11:34 AM

Hi,

I also stuck in the same problem and your solution work for me too. Although exception don't handle by the exception block.

I define the exception policy that wrap the exception and message will be "Some My Custom Message.". When an exception

will be generate by my code it should be handled by the "Exception Application Block" and returned message should contains custom message.

But still same exception will be show at the UI with actual exception message.