我正在開發與asp.net mvc 2的Web應用程序,並且我正在使用NHibernate(會話每請求)和Unity(依賴注入)。解決方法與統一
在我的Global.asax我管理我的Isession是這樣的:
public override void Init()
{
base.Init();
BeginRequest += OpenNHibernateSession;
EndRequest += DisposeNHibernateSession;
}
private static void OpenNHibernateSession(object sender, EventArgs e)
{
ManagedWebSessionContext.Bind(HttpContext.Current, _sessionFactory.OpenSession());
}
private static void DisposeNHibernateSession(object sender, EventArgs e)
{
var session = ManagedWebSessionContext.Unbind(HttpContext.Current, _sessionFactory);
if (session != null)
session.Dispose();
}
尼斯,並在我的庫(在構造函數)我通過SessionFacotry(在Global.asax中靜)電子獲得的Isession通過SessionFacotry.GetCurrentSession()。我在儲存庫中注入了控制器的控制器!太棒了...
但是,在我的asp.net mvc應用程序中,我有一個緩存值和一個CacheRemovedItem委託與此緩存。在這種方法中(委託),我需要在我的reopsitories中保留一些信息,但是當緩存過期時我沒有請求。我想知道我怎麼能解決我的依賴在這個方法(CacheItemRemoved委託)正確的方式?或我如何設置一個ISession通過SessionFactory中的GetCurrentSession()獲取? PS:對不起,我的英文不好!
你在哪裏存放貨櫃?在HttpModule中?爲什麼會話工廠未通過容器解析? – onof 2010-09-12 07:18:08
HI Onof,global.asax上的容器和會話工廠是靜態的。我在Application_Start事件中初始化。在我的倉庫中,我通過GetCurrentSEssion獲得會話(因爲我通過ManagedWebSessionContext.Bind和ManagedWebSessionContext.Unbind爲每個請求設置了會話。所以,我需要在我的方法中設置這個嗎? – 2010-09-13 11:31:27