Я использую 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% случаев свойство Session
имеет значение 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).
У кого-нибудь есть идея, что еще я могу проверить?
Lg
вараппа