2011-05-23 51 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(编辑:它的工作原理)。