该工厂已被处置,无法再使用. NHibernate设施 [英] The factory was disposed and can no longer be used. NHibernatefacility

查看:76
本文介绍了该工厂已被处置,无法再使用. NHibernate设施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了三天,用Castle和wcf来解决NHibernatefacility的问题,而且确实令人沮丧.

I have been trying for three days to figure out this NHibernatefacility thing with Castle and wcf and it's really getting frustrating.

解决了很多错误之后,我发现了一个很明显的错误,但我无法解决.

After solving a good dozen of errors, i have come to this one which seems pretty obvious but i can't solve.

这是我的global.asax的Application_Start

This is my global.asax's Application_Start

        container.AddFacility<AutoTxFacility>();
        container.Register(Component.For<INHibernateInstaller>().ImplementedBy<NHibernateInstaller>());
        container.AddFacility<NHibernateFacility>();
        container.AddFacility<WcfFacility>();

        container.Register(
            Component.For<IAuthService>().ImplementedBy<AuthService>().LifestylePerWcfOperation(),
            Component.For<IUserRepository>().ImplementedBy<UserRepository>().LifestylePerWcfOperation(),
            Component.For<IActionWebService>().ImplementedBy<ActionWebService>().Named("ActionWebService").LifestylePerWcfOperation(),
            Component.For<ISession>().LifeStyle.PerWcfOperation().UsingFactoryMethod(x => container.Resolve<ISessionManager>().OpenSession()));

这适用于第一个请求.之后,它会出现此错误.

This works for the first request. After that, it comes up with this error.

工厂已被处置,无法再使用. 对象名称:"this".

The factory was disposed and can no longer be used. Object name: 'this'.

此行中我的用户存储库中发生错误

the error is happening in my userrepository in this line

[Transaction]
        public virtual User GetUserByCredentials(string email, string password)
        {
            using (var tx = session())
            {
                return tx.QueryOver<User>().Where(x => x.Email == email && x.Password == password).SingleOrDefault();
            }
        }

我感觉这与LIfestyle有关.我尝试了多种组合,但未成功.我目前不知道该怎么办.我使用所有设施(应该让生活更轻松)进入了Castle这个事物,由于缺乏文档,它真的很复杂.我还没有找到可以一起实现所有这些功能的血统指南,更不用说它还不到4年的历史.

I am having a feeling this has to do with the LIfestyle. I have tried multiple combinations but unsuccessful. I don't know what to do at this point. I got into this Castle thing with all the facilities (that are supposed to make life easier) and it's really complicated due to the lack of documentation. I haven't been able to find a descent guide to implement all of this together, not to mention something that is not 4 years old.

请帮助!

推荐答案

对不起,以前没有找到这个问题.

Sorry for not finding this question previously.

出现此错误消息的最可能原因是您正在重新注册ISession.该设施的重点是为您提供支持.

The most likely reason you're getting this error message is that you are re-registering the ISession. The point of the facility is to provide that support for you.

我还在您的评论中看到,您已将ISession设置为单例.永远不要这样做,因为它上的任何单个故障都将导致崩溃和烧伤,并且您必须丢弃整个容器(通常是应用程序的合成根目录,因此必须重新启动应用程序)

I also read in your comment that you have set the ISession to singleton. That should never ever be done, because any single fault on it an you will crash and burn and you'll have to throw away the full container (which most often is the composition root of the application, so you have to reboot the application).

该工具的重点是为您提供基于AOP的事务,然后您需要使事务尽可能靠近GUI或命令层.子操作(例如读取)不应使用[Transaction]包裹在单个事务中,因为它们并不表示您的操作的事务边界.

The point of the facility is to give you AOP-based transactions, and then you need to have your transactions as close to the GUI or command layer as possible. Child operation, such as reading should not be wrapped in singular transactions with [Transaction] because they do not denote the transactional boundary for your operation.

相反,请查看您的Surface API,看看您在哪里调用了应该与ACID一起运行的方法;在这里放置属性.

Instead, look at your surface API and see where you have methods being called that are supposed to run with ACID; this is where you put the attributes.

就您而言,您的事务界限似乎在WCF调用周围.您需要做的是取代注册ISession的生活方式.

In your case, it seems that your transactional boundaries are around the WCF calls. What you'd need to do is to replace the lifestyle that the ISession is registered with.

如果您看看NHibernateFacility的功能,您将找到一种改变短暂生活方式的选择;如果取决于ISession的所有组件都是瞬态的,那么您最好在ISession上使用瞬态"生活方式,因为这样可以保证它只能存在于从合成根目录/容器中提取的对象生活.

If you have a look at the c'tor for NHibernateFacility, you'll find an option to pass transient lifestyle; if all of your components depending on ISession are transient you'll be good to go with Transient lifestyle on ISession, because it is guaranteed to only live for as long as the object taken from the composition root/container lives.

真正"的解决方法是从我的github扩展该功能,在c'tor中的枚举取一个PerWCFOperation值,并用这些功能向ISessionManager和Func注册该功能,类似于对这三个功能的处理方式现有的生活方式.

The 'real' fix is to extend the facility, from my github, with an enum in the c'tor taking a PerWCFOperation value, and having the facility register ISessionManager and Func with those, similar to how it does with the three existing lifestyles.

这篇关于该工厂已被处置,无法再使用. NHibernate设施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆