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
没有找到相关结果
已邀请:
2 个回复
仇聘发栖
辟官陡板休
移到
,如此处指出的那样。 注意: 也许应该将此代码移到OnAuthorization中的基本控制器中(编辑:它可以工作!)。