为 Web 应用程序设置 Fluent NHibernate 和 StructureMap
我使用这个方法 http://www.kevinwilliampang.com/2010/04/06/setting-up-asp-net-mvc-with- Fluent-nhibernate-and-structuralmap/ 用于使用结构图设置 fnh 但之后一个请求我收到以下异常
会话已关闭! 对象名称:“ISession”。
描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.ObjectDisposeException:会话已关闭! 对象名称:“ISession”。
我的存储库类如下所示:
public class Repository : IRepository {
private readonly ISession _session;
public Repository(ISession session) {
_session = session;
}
public T Get<T>(Expression<Func<T, bool>> predicate) {
return _session.CreateCriteria(typeof(T)).Add(predicate).UniqueResult<T>();
}
我在结构图中注册我的存储库,如下所示:
public class RepositoryRegistry : Registry {
public RepositoryRegistry() {
Scan(a => {
a.AssembliesFromApplicationBaseDirectory();
a.AddAllTypesOf<IRepository>();
});
}
}
如何防止会话被关闭?
I use this approuch http://www.kevinwilliampang.com/2010/04/06/setting-up-asp-net-mvc-with-fluent-nhibernate-and-structuremap/ for setting up fnh with structuremap but after one request I get the following exception
Session is closed!
Object name: 'ISession'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ObjectDisposedException: Session is closed!
Object name: 'ISession'.
My repository class looks like this:
public class Repository : IRepository {
private readonly ISession _session;
public Repository(ISession session) {
_session = session;
}
public T Get<T>(Expression<Func<T, bool>> predicate) {
return _session.CreateCriteria(typeof(T)).Add(predicate).UniqueResult<T>();
}
and I register my repository in structuremap like this:
public class RepositoryRegistry : Registry {
public RepositoryRegistry() {
Scan(a => {
a.AssembliesFromApplicationBaseDirectory();
a.AddAllTypesOf<IRepository>();
});
}
}
How can I prevent the session from being closed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否按照示例中的方式注册 ISession?它应该是 HttpContext 范围如下:
另一种可能性是某些东西正在注册为单例(并且保留关闭的会话,而不是使用当前会话重新创建。
在 StructureMap 列表上看到您的问题后: http://groups.google.com/group/structuralmap-users/browse_thread/thread /8023e0acc43ceeb3#,我看到了问题。
您正在将存储库注入到站点地图中,这是一个单例,因此您需要为每个请求提供一个新会话,如下所示:
Are you registering your ISession the same way they do in the example? It should be HttpContext scoped like so:
The other possibility is that something is getting registered as a singleton (and is holding onto a closed session, rather than being recreated with the current session.
After seeing your question on the StructureMap list: http://groups.google.com/group/structuremap-users/browse_thread/thread/8023e0acc43ceeb3#, I see the problem.
You are injecting your repository into the sitemap, which is a singleton. So you will need to give the SiteMap a new session every request like so: