为Web应用程序设置流畅的NHibernate和StructureMap [英] Setting up Fluent NHibernate and StructureMap for a web application

查看:170
本文介绍了为Web应用程序设置流畅的NHibernate和StructureMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个approuch http://www.kevinwilliampang.com/2010/04/06/setting-up-asp-net-mvc-with-fluent-nhibernate-and-structuremap/ 设置fnh结构映射,但一个请求后,我得到以下异常:



会话关闭!
对象名称:'ISession'。



描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪,了解有关错误的更多信息以及源代码的位置。

异常详细信息:System.ObjectDisposedException:Session已关闭!
对象名称:'ISession'。



我的资源库类如下所示:

  public class Repository:IRepository {
private readonly ISession _session;
public Repository(ISession session){
_session = session;
返回_session.CreateCriteria(typeof(T)).Add(predicate).UniqueResult< T>(表达式< Func< T,bool>>谓词) ; T>();





我在结构映射中注册我的仓库如下所示:

$ b ($ a
$ b $ $ $ $ $ $ $ $ $ $ $ $ $) AssembliesFromApplicationBaseDirectory();
a.AddAllTypesOf< IRepository>();
});




$ b $ p
$ b

如何防止会话被关闭? / p>

解决方案

您是否像在示例中那样注册您的ISession?它应该是HttpContext作用域如下所示:

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

另一种可能性是某个东西被注册为一个singleton(并且正在保持一个关闭的会话,而不是在当前会话中重新创建。



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

您正在将您的资源库注入到sitemap中,因此您需要为每个请求提供一个新的会话,如下所示:

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


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


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?

解决方案

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>();
}

这篇关于为Web应用程序设置流畅的NHibernate和StructureMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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