У меня есть WebSite с пользовательским объектом Cache внутри библиотеки классов. Все проекты запущены .NET 3.5. Я хотел бы преобразовать этот класс для использования состояния сеанса вместо кеша, чтобы сохранить состояние в сервере состояний, когда мое приложение перезагружается. Однако этот код генерирует исключение, когда "HttpContext.Current.Session равно null", когда я посещаю методы из моего файла Global.asax. Я вызываю класс следующим образом:
Customer customer = CustomerCache.Instance.GetCustomer(authTicket.UserData);
Почему объект allways null?
public class CustomerCache: System.Web.SessionState.IRequiresSessionState
{
private static CustomerCache m_instance;
private static Cache m_cache = HttpContext.Current.Cache;
private CustomerCache()
{
}
public static CustomerCache Instance
{
get
{
if ( m_instance == null )
m_instance = new CustomerCache();
return m_instance;
}
}
public void AddCustomer( string key, Customer customer )
{
HttpContext.Current.Session[key] = customer;
m_cache.Insert( key, customer, null, Cache.NoAbsoluteExpiration, new TimeSpan( 0, 20, 0 ), CacheItemPriority.NotRemovable, null );
}
public Customer GetCustomer( string key )
{
object test = HttpContext.Current.Session[ key ];
return m_cache[ key ] as Customer;
}
}
Как вы можете видеть, я попытался добавить IRequiresSessionState в класс, но это не имеет значения.
Приветствия Jens