【问题标题】:Caching not working with IIS?缓存不适用于 IIS?
【发布时间】:2010-11-12 07:34:18
【问题描述】:

全部,

我有一个 ASP.NET(C#),它可以与集成的调试器/Web 服务器一起按预期运行。但是,当我将它移到 IIS 服务器时,它看起来好像没有设置缓存对象。任何人都可以提供任何帮助吗?

这是设置缓存和后续 cookie 的类。

   class globals
    {
        public NameValueCollection values;
        private static string m_visitNumber ="";

    public globals()
    {
        string userName = HttpContext.Current.Request.Cookies["PatientDischargeSummary"].Value;
        values = HttpContext.Current.Cache[userName] as NameValueCollection;
    }

    public globals(NameValueCollection form)
    {
        // Copy the form values.
        values = new NameValueCollection();
        values.Add("txtCR", form["txtCR"]);
        values.Add("txtName", form["txtName"]);



        // Add the values to the cache.
        //HttpContext.Current.Cache.Insert(form["txtUserName"], values, null, System.Web.Caching.Cache.NoSlidingExpiration, TimeSpan.FromMinutes(5));
        HttpRuntime.Cache.Insert(form["txtUserName"], values, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
        //HttpContext.Current.Cache.Insert(form["txtUserName"], values, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);

        // Add the username to the cookies.
        HttpCookie cookie = new HttpCookie("PatientDischargeSummary", form["txtUserName"]);
        cookie.Expires = DateTime.Now.AddMinutes(30);
        cookie.HttpOnly = true;
        HttpContext.Current.Response.Cookies.Add(cookie);
    }

我使用缓存的一个例子:

globals pcs;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            pcs = new globals();


            lblActiveEditor.Text = pcs.values["txtName"];


        }

    }

在IIS下产生如下错误:

[NullReferenceException:对象引用未设置为对象的实例。] Demographics.ascx.cs:23 中的 navigationtest.Demographics.Page_Load(Object sender, EventArgs e) System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,对象 o,对象 t,EventArgs e)+15 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者,EventArgs e)+34 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +47 System.Web.UI.Control.LoadRecursive() +131 System.Web.UI.Control.LoadRecursive() +131 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

有什么想法吗?

【问题讨论】:

    标签: c# asp.net iis


    【解决方案1】:
    pcs.values["txtName"]
    

    这是空的,当您尝试接收它时它已经从缓存中消失了。您的代码似乎缓存了一些从用户输入中获得的每个请求的数据,并且没有任何东西可以保证这些数据在您的缓存中的可用性。

    每个缓存访问都应该准备好从数据源获取数据以防丢失,所以,在你的情况下,我会使用用户的会话(虽然我不知道你的架构,有多少服务器.. .) 使用用户的会话将在 appdomain 或会话本身的生命周期内保留该数据(以先结束者为准),因此您还应该准备好再次查询它,以防错过/超时/appdomain 关闭。

    【讨论】:

    • 我在启动时填充缓存: if (Request["txtUserName"] != null) { userName = Request["txtUserName"]; // 使用来自 PCS 的初始表单值填充缓存。 pcs = new globals(Request.Form); } else { userName = Request.Cookies["PatientDischargeSummary"].Value;个人电脑 = 新的全局变量(); Response.Write(用户名); } 但是为什么这会在调试模式而不是 IIS 下工作?
    • 我猜你所说的“启动”是指你的登录页面。就像我说的,如果这些数据是特定于用户的,并且您不需要在该用户的请求代码之外访问它,您应该考虑将其存储在用户的会话中。
    【解决方案2】:

    您应该使用 HttpContext.Cache 而不是 HttpRuntime.Cache 吗?

    【讨论】:

    • 是的 - 这就是我最初的想法,但我遇到了同样的问题。
    • 第一个是第二个的快捷方式。
    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 2015-07-12
    • 2017-03-29
    • 2015-05-29
    • 2014-10-19
    • 2018-02-22
    • 2016-04-25
    • 1970-01-01
    相关资源
    最近更新 更多