我在風中撲動,所以我想我會在這裏問...請讓我知道,如果這是顯而易見的,並已答覆之前。NHibernate投擲課程已關閉
我正在建立一個MVC 3網站,該網站在我用一個用戶運行它時正常工作,在這裏我點擊了頁面。但是,如果我瘋狂地刷新,最終我打了一個「會議閉幕」。
我已經隔離出了幾乎所有的存儲庫以嘗試進入底部,所以我知道它在主頁上出現錯誤。唯一在存儲庫中調用的是從數據庫表中獲取用戶名。
我使用Postgres作爲數據庫,以及NauckIT的ASP.NET成員資格提供程序。主數據庫也是Postgres(但另一個數據庫)。
會話管理使用下面的代碼完成:
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();
}
}
是獲得代碼的登錄信息是:
public Login GetCurrentLogin()
{
return Session.Query<Login>().FirstOrDefault(l => l.UserID == UserAccessRepository.UserID);
}
的UserAccessRepository
只是獲取從窗體身份驗證cookie的userid
。
ninjectKernel.Bind<IUserRepository>().To<NHUserRepository>();
ninjectKernel.Bind<ILeagueRepository>().To<NHLeagueRepository>().InThreadScope();
ninjectKernel.Bind<ISession>()
.ToMethod(m => MvcApplication.SessionFactory.GetCurrentSession())
了一個SessionFactory來自:
會話使用注入倉庫
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;
需要明確的是,它在標準情況下,我在頁面中單擊工作正常,但當我瘋狂地擊中F5時,我得到了會議結束的問題。
更新: 不知道它是否相關,但我看到這個在BaseController
,從OnActionExecuting
方法的主要地方。這似乎已經清除了上述方法。
我的第一個問題是:爲什麼整個'開始/ EndRequest'設置,而不是注入每個會話的生命期範圍'ISession'到控制器? –
第一個例子我發現會議每個請求...高興替代..有鏈接? – Martin
@ Trustme-I'maDoctor這是一種非常常見的做法......不要說這是最好的方式,但我絕對能看到它遍佈整個地方。 – BlakeH