NHibernate Closing Closed

I am clapping in the wind, so I thought I would ask here ... Please let me know if this is obvious, and it was answered earlier.

I am creating an MVC 3 site that works great when I run it with a single user, where I view pages. However, if I frantically hit the update, over time I hit "Session closed."

I have isolated almost all of my repository to try and figure it out, so I know this is a bug on the main page. The only thing that is called in the repository is to get the username from the database table.

I use Postgres as a database and ASP.NET membership provider from NauckIT. The main database is also Postgres (but another database).

Session management is performed using the following code:

public class MvcApplication : System.Web.HttpApplication { public static ISessionFactory SessionFactory = NHibernateHelper.GetSessionFactory(); public MvcApplication() { this.BeginRequest += MvcApplication_BeginRequest; this.EndRequest += MvcApplication_EndRequest; } void MvcApplication_BeginRequest(object sender, EventArgs e) { CurrentSessionContext.Bind(SessionFactory.OpenSession()); } void MvcApplication_EndRequest(object sender, EventArgs e) { CurrentSessionContext.Unbind(SessionFactory).Dispose(); } } 

Code that receives login information:

  public Login GetCurrentLogin() { return Session.Query<Login>().FirstOrDefault(l => l.UserID == UserAccessRepository.UserID); } 

UserAccessRepository simply gets the userid from the forms authentication cookie.

The session is entered into the repository using:

  ninjectKernel.Bind<IUserRepository>().To<NHUserRepository>(); ninjectKernel.Bind<ILeagueRepository>().To<NHLeagueRepository>().InThreadScope(); ninjectKernel.Bind<ISession>() .ToMethod(m => MvcApplication.SessionFactory.GetCurrentSession()) 

Sessionfactory comes from:

 public class NHibernateHelper { private static ISessionFactory _sessionFactory; public static ISessionFactory SessionFactory { get { if (_sessionFactory == null) { var rawConfig = new Configuration(); rawConfig.SetNamingStrategy(new PostgresNamingStrategy()); var configuration = Fluently.Configure(rawConfig) .Database(PostgreSQLConfiguration.Standard.ConnectionString(ConnectionString).ShowSql().Dialect("NHibernate.Dialect.PostgreSQL82Dialect")) .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Event>(new AutoMapConfiguration()) )).ExposeConfiguration(cfg => cfg.SetProperty("current_session_context_class", "web") _sessionFactory = configuration.BuildSessionFactory(); Debug.WriteLine("Built SessionFactory"); } return _sessionFactory; 

To be clear, it works fine in the standard instance, where I click on the pages, but when I hit F5 frantically, I get a closed session.

UPDATE: Not sure if this is relevant, but the main place that I see in BaseController is from the OnActionExecuting method. He seems to have made it clear in the above methods.

+4
source share
1 answer

You should not use InThreadScope() in a web application. Use InRequestScope() . EDIT Read Object Scopes - it was recently updated and without knowing it, sooner or later it will waste your time!

If you want to get the work to work through the membership provider and handle the requests, you need to find the Ninject Custom Provider (maybe something like here ).

+1
source

Source: https://habr.com/ru/post/1481824/


All Articles