如何在 NHibernate 中使用 MySQL 对多个请求进行多线程会话?

How to multi-thread a Session in NHibernate using MySQL for multiple requests?

我在我的 MVC C# 应用程序中使用 NInject MVC5 实现了 NHibernate。一切正常,但我意识到当我双击 links 时,我总是会收到错误消息: {"There is already an open DataReader associated with this Connection which must be closed first."}

{"Session is closed!\r\nObject name: 'ISession'."}

现在我知道这是因为我将我的 IContentService 接口绑定到 ISession。我也知道 ISession 不是线程安全的,只能使用一次。所以我的问题是......那么我应该如何创建一个可以跨多个线程同时使用 InRequestScope 的会话。因为我知道用户会双击或用户会同时单击 link 另一个用户正在单击相同的 link 并且阻止多个用户的错误并不是很理想。

我的注射中有这个:

 Bind<IContentService>().To<ContentService>().InRequestScope();

            Bind<ISession>() //I'm sure the problem is right here, but not sure.
               .ToMethod(
                   context =>
                       context.Kernel.Get<IMasterSessionSource>().GetSession())
               .WhenInjectedInto<IContentService>()
               .InRequestScope();

我的内容服务

public interface IContentService
    {
        IQueryable<Question> Questions{ get; }
    }


 public class ContentService : IContentService
    {
        private readonly ISession _session; //I think the problem might be here too...

        public ContentService(ISession session)
        {
            _session = session;
        }

        public IQueryable<Question> Questions
        {
            get { return _session.Query<Question>(); }
        }
    }

更新

我阅读了如何使用 ISessionFactory 进行多线程,但我无法像绑定 ISession 那样绑定它。

原来我是正确的多线程处理,每个请求都有会话。问题是 NHibernate 有一个主要问题 MySQL 过早关闭会话。感谢您的回复。这绝对是一个很难解释的问题。

更新

在这里找到答案: