Tuesday, September 6, 2011

Unity: Interception Call Handler called multiple times

For a project we are using Unity 2.0 interception with policy injection to create and manage our NHibernate session. Therefore we created a custom ICallHandler in combination with a HandlerAttribute.
[AttributeUsage(AttributeTargets.Method)]
    public class NHibernateUnitOfWorkHandlerAttribute : HandlerAttribute
    {
        private bool _createTransaction;

        public NHibernateUnitOfWorkHandlerAttribute(bool createTransaction=false)
        {
            _createTransaction = createTransaction;
        }

        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new NHibernateUnitOfWorkHandler(Order,_createTransaction);
        }
    }


    public class NHibernateUnitOfWorkHandler : ICallHandler
    {
        public NHibernateUnitOfWorkHandler(int order,bool createTransaction)
        {
            this.Order = order;
            this.CreateTransaction = createTransaction;
        }

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            IMethodReturn result=null;

            try
            {
                using (var uow = DataFactory.CreateUnitOfWork(TransactionMode.Explicit))
                {
                    if (CreateTransaction)
                    {
                        using (var tx = uow.CreateTransaction())
                        {
                            result = getNext()(input, getNext);
                            if (result.Exception == null)
                                tx.Commit();
                            else
                                tx.Rollback();
                        }
                    }
                    else
                    {
                        result = getNext()(input, getNext);
                    }
                }
            }
            catch (Exception ex)
            {
                result = getNext()(input, getNext);
                result.Exception = ex;
            }
            return result;
        }

        public int Order { get; set; }
        public bool CreateTransaction { get; set; }
    }


Problem is, that this policy injection call handler executed twice when we expect that it only executes once. It took us some time to figure out what caused the problem. The reason was that the InterceptionExtension was registered twice:
  • One time explicitly through registration by using the AddNewExtension<T> method on the Unity container.
     
  • One time implicitly by registering the EnterpriseLibraryCoreExtension which also adds the Interception extension.
After removing the explicit registration everything worked fine. To further improve the solution we started to use the AddNewExtensionIfNotPresent<T> extension method which is available in the Enterprise Library Shared Assembly.

3 comments:

Rick at KnowWare said...

Wow! This just saved me a ton of time and frustration. Thanks for taking the time to post it!

I am setting up a new development environment and using Unity in my unit tests as well (mostly to simplify setting up mocks). One of my unit tests verifies that policy injection is working. The test was passing when run by itself, but failing when run as part of a suite because the policy injection was occurring twice.

After finding and reading this post, I quickly traced it down to a double registration that was occurring in a static reference to an instance of Unity and was able to solve the problem easily.

Thanks again!

Anonymous said...

Multiple paths to registration have bitten me as well, I ended up following the whole process step-by-step with the debugger to find my faux pas!

MusicFanatic said...

Wow... So many headaches for something so trivial....

Thanks a lot for sharing your experience. Probably would never have figured it out for myself.