Unity Interceptions - AOP and Attributes

Jun 27, 2013 at 4:27 PM
Edited Jun 29, 2013 at 8: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 :
[AttributeUsage(AttributeTargets.Method)]
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
    {
        get
        {
            if (_Logger == null)
                _Logger = new mLogger();
            return _Logger;
        }
        set
        {
            _Logger = value;
        }
    }

    public IMethodReturn Invoke(IMethodInvocation input,
      GetNextHandlerDelegate getNext)
    {}
Container Setup and Registerations
    public void SetUp()
    {
        container.AddNewExtension<Interception>();

        //register types
        container.RegisterType<MainWindowViewModel>();
        container.RegisterType<DummyModel>();

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

        // Here is the important part !!!
        CustomAttributeMatchingRule ruleLog = new CustomAttributeMatchingRule(typeof(LoggingAttribute), true);            
        policyLog.AddMatchingRule(ruleLog)
                 .AddCallHandler<LoggingCallHandler>();       
    }
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.
http://sdrv.ms/11Q6VdW
The Question :

Why the Invoke in the Call Handler is never called ?
Jun 28, 2013 at 3:43 PM
Edited Jul 10, 2013 at 3: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
entlib.support@live.com
Enterprise Library support engineer
Support How-to .
Jun 29, 2013 at 7:35 AM
Edited Jun 29, 2013 at 8:35 PM
Thanks for the info.