为 Web 应用程序设置 Fluent NHibernate 和 StructureMap

发布于 2024-09-25 18:16:50 字数 1131 浏览 0 评论 0原文

我使用这个方法 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も星光 2024-10-02 18:16:50

您是否按照示例中的方式注册 ISession?它应该是 HttpContext 范围如下:

      x.For<ISession>()
        .HttpContextScoped()
        .Use(context => context.GetInstance<ISessionFactory>().OpenSession());

另一种可能性是某些东西正在注册为单例(并且保留关闭的会话,而不是使用当前会话重新创建。

在 StructureMap 列表上看到您的问题后: http://groups.google.com/group/structuralmap-users/browse_thread/thread /8023e0acc43ceeb3#,我看到了问题。

您正在将存储库注入到站点地图中,这是一个单例,因此您需要为每个请求提供一个新会话,如下所示:

public class MvcSiteMapProvider : SiteMapProvider { 
     public static IRepository Repository { get; set; }; 
     public MvcSiteMapProvider() { }
} 

protected void Application_BeginRequest() { 
     MvcSiteMapProvider.Repository = ObjectFactory.GetInstance<ISession>();
}

Are you registering your ISession the same way they do in the example? It should be HttpContext scoped like so:

      x.For<ISession>()
        .HttpContextScoped()
        .Use(context => context.GetInstance<ISessionFactory>().OpenSession());

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:

public class MvcSiteMapProvider : SiteMapProvider { 
     public static IRepository Repository { get; set; }; 
     public MvcSiteMapProvider() { }
} 

protected void Application_BeginRequest() { 
     MvcSiteMapProvider.Repository = ObjectFactory.GetInstance<ISession>();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文