HttpContextBase:会话为null

| 我使用Windor Castle通过工厂方法将HttpContext包装到HttpContextWrapper中。
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%的情况下,仅当请求的请求率很高时,“ 6”属性为null。 奇怪的是,如果我使用
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会话工厂除外)。 有人知道我还能检查什么吗? g Warappa     
已邀请:
我前段时间也遇到过类似的问题,也许我的问题的答案可以为您提供帮助:ASP.NET MVC和Windsor.Castle:使用与HttpContext相关的服务     
好的,这不是由于温莎城堡的注册不当,而是更简单的原因:我在不希望完全初始化Session的时候访问了Session-请假我! 我的解决方案是将Session访问代码从
Application_BeginRequest
移到
Application_AcquireRequestState
,如此处指出的那样。 注意: 也许应该将此代码移到OnAuthorization中的基本控制器中(编辑:它可以工作!)。     

要回复问题请先登录注册