【问题标题】:Error accessing the Cache in the Application_Start method of Global.asax在 Global.asax 的 Application_Start 方法中访问缓存时出错
【发布时间】:2008-11-06 15:42:18
【问题描述】:
我正在尝试访问 Global.asax 的 Application_Start 方法中的缓存。我正在尝试访问 Context.Cache。当我从 Visual Studio 运行代码时,它工作得很好,但是当我从 IIS 在服务器上运行它时,我得到:会话状态在此上下文中不可用。我想在应用程序启动时在缓存中植入一些值。如何从 Application_Start 获取缓存?
【问题讨论】:
标签:
c#
asp.net
iis
global-asax
【解决方案1】:
哇。我找到了答案。使用变量 HttpRuntime.Cache 而不是 Context.Cache。
【解决方案2】:
是的,这在 IIS7 中随着切换到集成模式而发生了变化。 Application_Start 执行较早,因此 HttpContext 不可用。
Visual Studio 使用 Cassini,这在几个方面与完整的 IIS 不同。查看新发布的IIS Express,它也可以与 Visual Studio 一起使用。它是 IIS 7.5 的独立版本,因此您不会遇到使用 Cassini 进行测试时不存在的生产问题。