Unity Interceptions - AOP and Attributes

Jun 27, 2013 at 5:27 PM
Edited Jun 29, 2013 at 9:49 AM
I'm experimenting on making an AOP-Based library, here is what I have and the problem

The Question :

Why the Invoke in the Call Handler is never called ?

Attribute :
public class LoggingAttribute : HandlerAttribute
    public override ICallHandler CreateHandler(IUnityContainer container)
        return new LoggingCallHandler();
Call Handler
public class LoggingCallHandler : ICallHandler {

    private mLogger _Logger;
    private mLogger Logger
            if (_Logger == null)
                _Logger = new mLogger();
            return _Logger;
            _Logger = value;

    public IMethodReturn Invoke(IMethodInvocation input,
      GetNextHandlerDelegate getNext)
Container Setup and Registerations
    public void SetUp()

        //register types

        PolicyDefinition policyLog = container.Configure<Interception>().
            new VirtualMethodInterceptor()).
            new VirtualMethodInterceptor()).AddPolicy("LoggingPolicy");

        // Here is the important part !!!
        CustomAttributeMatchingRule ruleLog = new CustomAttributeMatchingRule(typeof(LoggingAttribute), true);            
no doubt , in code :

lots of [Logging]s were used

I think I've posted the necessary parts, If there is anything missed inform me.

I have doubt on the "Matching Rules" and Interception part.
The Question :

Why the Invoke in the Call Handler is never called ?
Jun 28, 2013 at 4:43 PM
Edited Jul 10, 2013 at 4:59 PM
Thanks for providing a complete, working sample.

There are a few reasons why the call handler is not invoked.

The LoggingAttribute is applied on the MainWindow Window. There are 3 issues with that:
  • Interception is never configured for MainWindow
  • The methods in MainWindow are not virtual so would not be able to be intercepted even if configured
  • MainWindow is not instantiated by the container so a proxy object will not be generated and interception will not occur
However, even if you rectify all of the above it looks like you will hit a WPF issue: WPF: Deriving a class from a XAML-defined class in another module fails. Also see http://stackoverflow.com/questions/11226271/aop-with-ninject-interception-castle-dynamicproxy-and-wpf-window-cant-find-xa for a similar issue with ninject.

You should be able intercept on ViewModel classes, though.

Randy Levy
Enterprise Library support engineer
Support How-to .
Jun 29, 2013 at 8:35 AM
Edited Jun 29, 2013 at 9:35 PM
Thanks for the info.