【问题标题】:Why cant multiple users access my website?为什么多个用户无法访问我的网站?
【发布时间】:2016-02-04 16:23:35
【问题描述】:

我正在尝试使用 C# MVC nHibernate 构建网站。一切正常,但是当我在多个浏览器中浏览我的网站时出现错误:

已经有一个打开的 DataReader 与此 Connection 关联,必须先关闭它。

我已将我的许多服务绑定到一个会话 (ISession)。

        //ContentService Bingings
        Bind<IContentService>().To<ContentService>().InRequestScope();
        Bind<ISession>()
            .ToMethod(
                context =>
                    context.Kernel.Get<IMasterSessionSource>()
                        .ExposeConfiguration()
                        .BuildSessionFactory()
                        .OpenSession()
            )
            .WhenInjectedInto<IContentService>()
            .InSingletonScope();

        //GeneralService Bindings
        Bind<ISession>()
            .ToMethod(
                context =>
                    context.Kernel.Get<IMasterSessionSource>()
                        .ExposeConfiguration()
                        .BuildSessionFactory()
                        .OpenSession())
            .WhenInjectedInto<IGeneralService>()
            .InSingletonScope();

        Bind<IGeneralService>()
            .To<GeneralService>()
            .InSingletonScope();

        //CrossSellService Bindings
        Bind<ISession>()
            .ToMethod(
                context =>
                    context.Kernel.Get<IMasterSessionSource>()
                        .ExposeConfiguration()
                        .BuildSessionFactory()
                        .OpenSession())
            .WhenInjectedInto<ICrossSellService>()
            .InSingletonScope();

        Bind<ICrossSellService>()
            .To<CrossSellService>()
            .InSingletonScope();


        //Details Bindings
        Bind<ISession>()
                       .ToMethod(
                           context =>
                               context.Kernel.Get<IMasterSessionSource>()
                                   .ExposeConfiguration()
                                   .BuildSessionFactory()
                                   .OpenSession())
                       .WhenInjectedInto<IDetailsService>()
                       .InRequestScope();

        Bind<IDetailsService>()
            .To<DetailsService>()
            .InRequestScope();

他们被要求用于其他用途。例如我的 DetailsS​​ervice:

公共接口IDetailsS​​ervice { //在这里覆盖;
}

public class DetailsService : IDetailsService
{
    private static IContentService Context { get; set; }

    public DetailsService(IContentService context)
    {
        Context = context;
    }
 ...

其中 IContentService 是我的主要查询类:

 public interface IContentService
 {

    IQueryable<Source> Sources { get; }
  }

public class ContentService : IContentService
{
    private readonly ISession _session;
    public ContentService(ISession session)
    {
        _session = session;
    }

   public IQueryable<Source> Sources
    {
        get { return _session.Query<Source>(); }
    }

不知道为什么它不访问多个查询

【问题讨论】:

    标签: c# asp.net-mvc nhibernate


    【解决方案1】:

    看起来发生这种情况的原因是因为您试图在不同的线程中使用相同的 ISession。它这样做的原因是因为:

    Bind<ISession>()
            ...
            .InSingletonScope();
    

    您的会话不应该是网络环境中的单例。我建议使用 nhibernate 上下文会话。

    Why does Nhibernate share the session across multiple requests in my MVC application?

    【讨论】:

    • 我将服务更改为 InSingtonScope() 但它仍然给我错误
    • 显然我读到它与多线程 ISession 有关。
    【解决方案2】:

    这听起来很随意,但我的一位同事在我们支持的应用程序中遇到了类似的问题。他说这个问题与使用单例有关。 它已通过更改为瞬态修复。

    【讨论】:

      猜你喜欢
      • 2022-11-25
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 2016-03-29
      相关资源
      最近更新 更多