2011-05-23 29 views
0

我使用Windor城堡經由工廠方法來包裝的HttpContextHttpContextWrapperHttpContextBase:會話爲null

container.Register(
    Component.For<HttpContextBase>() 
     .LifeStyle.PerWebRequest 
     .UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current))); 

我有一個叫SessionStorage類訪問HttpContext.Current.Session。我註冊這樣說:

container.Register(
    Component.For<ISessionStorage>() 
    .LifeStyle.PerWebRequest 
    .ImplementedBy<HttpSessionStorage>()); 

HttpSessionStorage類:

public class HttpSessionStorage : ISessionStorage 
{ 
    public HttpContextBase httpContext { get; set; } 

    public void Remove(string key) 
    { 
     httpContext.Session.Remove(key);   
    } 

    public T Get<T>(string key) 
    { 
     return (T)httpContext.Session[key]; 
    } 

    public void Set<T>(string key, T value) 
    { 
     httpContext.Session[key] = value; 
    } 
} 

當我使用這種方式,那麼在約40%的情況下Session屬性null and only if 請求的價格非常高

奇怪的是,如果我使用的HttpContext.Current代替httpContext,它適用於所有情況。

public class HttpSessionStorage : ISessionStorage 
{ 
    public HttpContextBase httpContext { get; set; } 

    public void Remove(string key) 
    { 
     HttpContext.Current.Session.Remove(key);   
    } 

    public T Get<T>(string key) 
    { 
     return (T)HttpContext.Current.Session[key]; 
    } 

    public void Set<T>(string key, T value) 
    { 
     HttpContext.Current.Session[key] = value; 
    } 
} 

它是與溫莎城堡,但我不能發現問題。我註冊了我可以作爲PerWebRequest(NHibernate會話工廠除外)的所有內容。

有人有一個想法我可以檢查什麼?

了Lg
warappa

+0

你能定義「非常高的請求率」嗎?你能發佈一個失敗的測試嗎?你是否試過讓它暫時而不是PerWebRequest? – 2011-05-23 13:23:03

+0

「非常高的請求率」:ImageController必須提供〜25張圖片。每次向任何控制器發出請求(也是這樣),主體將創建並存儲在會話中(通過HttpSessionStorage)。就像上面說的那樣,有時保存會失敗,因爲包裝中會話中的會話爲空。 – 2011-05-23 18:46:18

回答

0

OK,這不是由於inproper溫莎城堡註冊,但更簡單的東西:我訪問會話當它的不保證完全初始化 - 啞我!

我的解決方案是將會話訪問代碼從Application_BeginRequest移動到Application_AcquireRequestState,如pointed out here

注:
也許這段代碼也應移入基地控制器 - 在OnAuthorization(編輯:它的工作原理)。