我使用Windor城堡經由工廠方法來包裝的HttpContext成HttpContextWrapper。HttpContextBase:會話爲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
你能定義「非常高的請求率」嗎?你能發佈一個失敗的測試嗎?你是否試過讓它暫時而不是PerWebRequest? – 2011-05-23 13:23:03
「非常高的請求率」:ImageController必須提供〜25張圖片。每次向任何控制器發出請求(也是這樣),主體將創建並存儲在會話中(通過HttpSessionStorage)。就像上面說的那樣,有時保存會失敗,因爲包裝中會話中的會話爲空。 – 2011-05-23 18:46:18