使用 Windsor 和 NHibernate 配置应用程序的 Asp MVC 问题
我在使用温莎、设施和 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() ;
}
......
}
Im having issues configuring application using windsor, facilities and nhibernate.
Im getting this exception:
ObjectDisposedException: Session is closed
Shouldnt windsor take care of instantiating session per request and opening it when I have configuration like this? Could I miss some configuration?
Here is my confuguration:
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() ;
}
......
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的
Repository
在其构造函数中获取 ISession 并且它是单例(默认生活方式),那么它仅在您调用存储库的第一个请求中起作用。在后续请求中,存储库仍将具有与第一次调用中相同的 ISession(因为存储库是单例),但该会话现在已关闭并且无法使用,因此您会看到错误。这就是为什么大多数时候您不希望单例依赖于具有“较短”生命周期(例如每个网络请求或瞬态)的其他组件。
请参阅本文,对常见生活方式问题进行更全面的分析。
If your
Repository<T>
gets a ISession in its constructor and it's singleton (default lifestyle), then it will only work in the first request you call your repository. In subsequent requests the repository will still have the same ISession as in the first call (because repository is singleton), but that session is now closed and invalid to use, therefore the error you see.This is why most of the time you don't want a singleton depending on other components with "shorter" lifestyles (like per-web-request or transient).
See this article for a more thorough analysis of common lifestyle issues.
我知道出了什么问题了。我忘记将我的存储库生活方式配置为 Transient。我不太明白这是怎么一个问题。
我想知道默认的生活方式是什么?我在文档中读到它是单例?!这怎么会成为问题呢?
I figured out what was wrong. I forgot to configure my repository lifestyle to Transient. I dont quite understand how this is a problem though.
I wonder what is the default lifestyle then? I was reading in docs that it is singleton?! How could that be a problem?