0

I'm trying to build a website using C# MVC nHibernate. All works well, but when i navigate my website in multiple browsers i get the error:

There is already an open DataReader associated with this Connection which must be closed first.

I have binded many of my services to one session (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();

and they get called for other uses. for example my DetailsService:

public interface IDetailsService { //OVERRIDES HERE;
}

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

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

where IContentService is my main Query class:

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

not sure why its not accessing multiple queries

NeoSketo
  • 525
  • 1
  • 7
  • 26

2 Answers2

2

This is going to sound very random, but a colleague of mine had a similar issue in the application we support. He said the issue was related to using a Singleton. It was fixed by changing to transient.

Baaleos
  • 1,703
  • 12
  • 22
2

The reason it looks like this is happening is because you are trying to use the same ISession across different threads. The reason it's doing this is because of this:

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

Your session should not be a singleton in a web environment. I would recommend using nhibernate contextual sessions.

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

Community
  • 1
  • 1
Cole W
  • 15,123
  • 6
  • 51
  • 85