Custom Handler to use with Policy Injection Application Block

Previous Posts:

Part 1 – Basic of IoC unity container
Part 2 – Basic of resolving dependencies and configure objects.
Part 3 – AOP with Policy Injection Application Block

In this fourth part I’ll examine how to build a simple custom handler that will be used with Policy Injection Application Block. The purpose of the handler is simply dumping to console the name of the called method as well all the parameters with theirs values. Here is the class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[ConfigurationElementType(typeof(CustomCallHandlerData))]
public class MyLogHandler : ICallHandler
{
   public MyLogHandler(NameValueCollection parameters)
   {
      this.parameters = parameters;
   }
   private NameValueCollection parameters;

   public int Order { get; set; }

   public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
   {
     // return input.CreateMethodReturn(null);
       
      Console.WriteLine("intercepted call to: {0}", input.MethodBase.Name);
      Console.WriteLine("Parameters:");
      for (int I = 0; I < input.Arguments.Count; I++)
      {
         Console.WriteLine("{0}: {1}", input.Arguments.ParameterName(I), input.Arguments[I]);
      }
      Console.WriteLine("Call original function");
      IMethodReturn retvalue = getNext()(input, getNext);
      return retvalue;
   }
}

Code is really simple, the only interesting method is the Invoke() that gets called instead of the original method of the wrapped object. This method has two parameters, the first contains all the informations on the method invocation, like: name and value of all parameters; the second parameter is a delegate used to get the next handler delegate in the AOP chain. This is needed because you can add more than one handler to a single method, so you will end up with a chain of interceptors.

Now we need to configure PIAB to insert this handler wherever we need it

image

I want to set a matching rule for an entire type (red), this type of matching rule has only one property called matches (Green) that will accepts a list of types to intercept, in this example I ask to add handlers only to the ITest interface (blue)

image

I need to specify a custom handler (red) as handler type, then configure the exact type in the type property of the handler (blue) the Order property can be left to 0 (automatic). You can use the order property to manually define the order in witch the handlers will be called.

Now I can simply fire this code

1
2
3
4
ITest test1 = container.Resolve<ITest>("OtherTest");
ITest wrapped = Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Wrap<ITest>(test1);
Console.WriteLine(wrapped.DoAnotherThing("TEST", 6));
Console.WriteLine(wrapped.DoSomething(2));

And gets this output.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
intercepted call to: DoAnotherThing
Parameters:
test: TEST
parami: 6
Call original function
Prop = VAL Names = 2
intercepted call to: DoSomething
Parameters:
param: 2
Call original function
SUPER:DoSomething Called
1

From the output you can notice that each invocation of methods belonging to ITest, will result in a call to our custom handler.

Inside an handler you really have great control on what to do, you are not only limited to passive operation like inspecting parameters but you can actively modify the behavior of the function. Let’s show how to swallow all exceptions, and logging them to the console.

1
2
3
4
5
6
7
IMethodReturn retvalue = getNext()(input, getNext);
if (retvalue.Exception != null)
{
   Console.WriteLine("Exception: {0}", retvalue.Exception);
   retvalue.Exception = null;
}
return retvalue;

With this code the handler first verify if the invocation of the function have raised an exception, if yes it simply dumps the exception message to the console and clear the exception. Now if you add a throw statement in DoAnotherThing function the output will be:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
intercepted call to: DoAnotherThing
Parameters:
test: TEST
parami: 6
Call original function
Exception: System.Exception: TEST
   at PolicyInjection.TestB.DoAnotherThing(String test, Int32 parami) in C:\Deve
lop\SvnGianMaria\CodeCommon\trunk\Experiment\PolicyInjection\PolicyInjection\Tes
tB.cs:line 21

intercepted call to: DoSomething
Parameters:
param: 2
Call original function
SUPER:DoSomething Called

As you can see the execution of the program was not stopped by the exception and the exception is dumped to the console.

In PIAB there is already a handler called Exception Handling Handler that is integrated with Exception Handling Application Block, and is a good starting point in defining centralized rules to handle the exception of applications. But if you need some custom logic you can write your own.

Download code.

alk.

Tags: IoC Policy Injection Application Block .NET Framework