I have implemented NHibernate using NInject MVC5 in my MVC C# application. All works well but i realized that when i double click on my links i always get the error(s): {"There is already an open DataReader associated with this Connection which must be closed first."}
or
{"Session is closed!\r\nObject name: 'ISession'."}
Now i know this is because im binding my IContentService interface to ISession. I also know that ISession is not thread-safe and can only be used once. So my question is... then how am i supposed to create a session that can be used simultaniously InRequestScope across multiple threads. Because i know users will double click or users will click a link at the same time another user is clicking the same link and errors that stop multiple users is not really ideal.
I have this in my injections:
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();
My ContentService
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>(); }
}
}
UPDATE
I read how i should use ISessionFactory for multithreading but i was unable to bind it like i can ISession.