Unity Interception and Custom ICallHandler – Order Property and Firing of Handlers

Last night I was up to the wee hours of the morning finishing some AOP examples for my Aspect-Oriented Programming Presentation to the Sarasota .NET Developer Group this Thursday, January 22, 2009.


As I was converting some custom Interceptors used in my various DynamicProxy and Castle Windsor samples to Unity HandlerAttributes and ICallHandlers, I was at a loss for why the handlers were not firing in the order in which I had marked them on the attributes.


For example, let’s say I am using Unity and its VirtualInterceptor to intercept a method on a class for which I have added several custom HandlerAttributes:


 



public class OrderDAO


{


    [Cache(Order = 2)]


    [Authorization("Administrator", Order = 1)]


    public virtual Order FetchById(int id)


    {


        // Do Something…


    }


}


 


The problem here was that while debugiing Authorization was not firing before Caching even though I had properly set the order.


 


Here is some example stub code for the AuthorizationAttribute:


 



public class AuthorizationAttribute : HandlerAttribute


{


    public string Role { get; set; }


 


    public override ICallHandler CreateHandler(IUnityContainer container)


    {


        return new AuthorizationCallHandler();


    }


}


 


Now the answer is obvious why the handlers were firing randomly, but at 2am it wasn’t so obvious until after about 15 minutes. I had forgotten to pass the value of the Order Property on the Attribute to the AuthroizationCallHandler. It is the value of the Order Property on the custom ICallHandler that determines the order in which handlers will fire, not the value on the attribute. Duh… So, it could be something like:


 



public class AuthorizationAttribute : HandlerAttribute


{


    public string Role { get; set; }


 


    public override ICallHandler CreateHandler(IUnityContainer container)


    {


        return new AuthorizationCallHandler(base.Order);


    }


}


 


and then make sure you set the order in the custom handler:


 



public class AuthorizationCallHandler : ICallHandler


{


    public int Order { get; set; }


 


    public AuthorizationCallHandler(int order)


    {


        Order = order;


    }


 


    // …


}


 


Again, this is obvious stuff I have done correctly a hundred times, but it is often the little things like this that can plague you at times. Hopefully if you get stuck on this problem a quick Google Search will get you here and help you solve the problem ;)


Here are some tutorials I have written in the past on creating HandlerAttributes and ICallHandlers:



 


David Hayden


 

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>