Cyclic dependency with Castle Windsor IoC for NHibernate ISession

前端 未结 1 1756
小鲜肉
小鲜肉 2020-12-19 20:50

I am using Castle Windsor for my IoC along with NHIbernate in an ASP.NET MVC app. It works great registered as follows (with one exception):

container.Regist         


        
相关标签:
1条回答
  • 2020-12-19 21:25

    Try to add a dependency on Func< ISession > in your interceptor class

    public class CustomInterceptor : EmptyInterceptor
    {
        private readonly Func<ISession> sessionFunc;
        private ISession session;
    
        protected ISession Session
        {
            get
            {
                return session ?? (session = sessionFunc());
            }
        }
    
        public CustomInterceptor(Func<ISession> sessionFunc)
        {
            this.sessionFunc = sessionFunc;
        }
    }
    

    And registration:

    container.Register(Component.For<ISession>().
        LifestylePerWebRequest()
        .UsingFactoryMethod(container =>
        {
            var interceptor = container.Resolve<IInterceptor>();
            return container.Resolve<ISessionFactory>.OpenSession(interceptor);
        }));
    container.Register(Component.For<Func<ISession>>()
        .LifestylePerWebRequest()
        .UsingFactoryMethod(container =>
        {
            Func<ISession> func = container.Resolve<ISession>;
            return func;
        }));
    
    0 讨论(0)
提交回复
热议问题