Unity & PIAB & Exception Handler

Topics: Exception Handling Application Block, Policy Injection Application Block
Apr 7, 2009 at 6:43 PM
Hello,

I was able to use Unity & PIAB to add a Logging handler to every method in my business class. I then tried to add the Exception handler to the same class, but when I cause an exception nothing happens.

<

 

policyInjection>

 

<

 

policies>

 

<

 

add name="Policy">

 

<

 

matchingRules>

 

<

 

add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.NamespaceMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

 

name="Namespace Matching Rule">

 

<

 

matches>

 

<

 

add match="Futrak2.Server.Repository" ignoreCase="false" />

 

</

 

matches>

 

</

 

add>

 

</

 

matchingRules>

 

<

 

handlers>

 

<

 

add logBehavior="BeforeAndAfter" beforeMessage="Before Method"

 

 

 

afterMessage="After Method" eventId="0" includeParameterValues="true"

 

 

 

includeCallStack="true" includeCallTime="true" priority="-1"

 

 

 

severity="Information" order="1" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

 

name="Logging Handler">

 

<

 

categories>

 

<

 

add name="MethodTrace" />

 

</

 

categories>

 

</

 

add>

 

<

 

add exceptionPolicyName="Exception Policy" order="0" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

 

name="Exception Handling Handler" />

 

</

 

handlers>

 

</

 

add>

 

</

 

policies>

 

</

 

policyInjection>
...

 

<

 

exceptionHandling>

 

<

 

exceptionPolicies>

 

<

 

add name="Exception Policy">

 

<

 

exceptionTypes>

 

<

 

add type="System.InvalidOperationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

 

 

 

 

 

postHandlingAction="None" name="InvalidOperationException">

 

<

 

exceptionHandlers>

 

<

 

add logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"

 

 

 

 

 

formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

 

 

 

priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

 

 

 

name="Logging Handler" />

 

</

 

exceptionHandlers>

 

</

 

add>

 

</

 

exceptionTypes>

 

</

 

add>

 

</

 

exceptionPolicies>

 

</

 

exceptionHandling>

 


Using

 

Container As IUnityContainer = New UnityContainer

 

 

'Add Interception to the container

 

Container.AddNewExtension(

Of EnterpriseLibraryCoreExtension)()

 

Container.AddNewExtension(

Of Interception)()

 

Container.AddNewExtension(

Of LoggingBlockExtension)()

 

Container.AddNewExtension(

Of ExceptionHandlingBlockExtension)()

 

 

'Get Policy Injection Settings from the Configuration

 

 

Dim ConfigSource As IConfigurationSource = ConfigurationSourceFactory.Create()

 

 

Dim PolicyInjectionSettings As PolicyInjectionSettings = CType(ConfigSource.GetSection(PolicyInjectionSettings.SectionName), PolicyInjectionSettings)

 

 

'Register Type with the container

 

Container.RegisterType(

Of IRepository(Of Employee), Repository(Of Employee))()

 

 

'Configure the container for Interception on given interface (IRepository(Of Employee) in this case)

 

 

'Here I am using TransparentProxyInterceptor, you can choose from

 

 

'InterfaceInterceptor, TransparentProxyInterceptor or VirtualMethodInterceptor, depending upon the requirement

 

Container.Configure(

Of Interception)().SetInterceptorFor(Of IRepository(Of Employee))(New InterfaceInterceptor())

 

 

If PolicyInjectionSettings IsNot Nothing Then

 

PolicyInjectionSettings.ConfigureContainer(Container, ConfigSource)

 

End If

 

 

Dim MainForm As TestForm = Container.Resolve(Of TestForm)()

 

Application.EnableVisualStyles()

Application.Run(MainForm)

 

End Using

 

Apr 8, 2009 at 3:15 AM

What is your expected behavior and what exactly happens?  If I was able to read your configuration correctly, the exception should not be visible to the user.


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

 

 

Apr 8, 2009 at 11:34 AM
Hi Sarah,

I was expecting the program to ignore the exception and to log it. Neither happen though.

Chanan.
Apr 9, 2009 at 1:25 PM
Does your program halts upon the throwing of the exception?  If this is the case, try pressing F10 and you'll see that it will continue the program execution.  This is what happens on my program.  The execution breaks on the exception but after pressing F10, it continues and was able to log the exception.  See this related thread - http://unity.codeplex.com/Thread/View.aspx?ThreadId=38655


Sarah Urmeneta

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


Apr 9, 2009 at 1:42 PM
Hi Sarah,

I am guessing that the problem is a bug in the Ent Lib as David Hayden pointed out in a blog post and I copied here: http://entlib.codeplex.com/Thread/View.aspx?ThreadId=52785

I will try it again now that I am using the work around