我在使用windsor,设施和nhibernate配置应用程序时遇到问题。
我得到了这个例外:
ObjectDisposedException: Session is closed
当我有这样的配置时,windsor不应该按照请求实例化会话并打开它吗?我可以错过一些配置吗? 这是我的承认:
public class PersistenceFacility : AbstractFacility
{
protected override void Init()
{
Configuration config = BuildDatabaseConfiguration();
Kernel.Register(
Component.For<ISessionFactory>()
.LifeStyle.Singleton
.UsingFactoryMethod(config.BuildSessionFactory),
Component.For<ISession>()
.LifeStyle.PerWebRequest
.UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession()));
}
private Configuration BuildDatabaseConfiguration()
{
return Fluently.Configure()
.Database(SetupDatabase)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<RnUlice>())
.ExposeConfiguration(ConfigurePersistence)
.BuildConfiguration() ;
}
......
}
答案 0 :(得分:7)
如果您的Repository<T>
在其构造函数中获得了一个ISession并且它是单例(默认生活方式),那么它只能在您调用存储库的第一个请求中工作。在后续请求中,存储库仍将具有与第一次调用相同的ISession(因为存储库是单例),但该会话现在已关闭且无效,因此您看到错误。
这就是为什么大多数时候你不想要单身取决于具有“更短”生活方式的其他组件(如每网络请求或瞬态)。
有关常见生活方式问题的详细分析,请参阅this article。
答案 1 :(得分:2)
我弄清楚出了什么问题。我忘了将我的存储库生活方式配置为Transient。我不太明白这是怎么回事。
container.Register(Component.For(typeof(IRepository<>))
.ImplementedBy(typeof(Repository<>)).LifeStyle.Transient);
我想知道什么是默认生活方式呢?我在文档中读到它是单身人士?!怎么会有问题?