本文介绍了为什么我的HttpContext.Application变量无法通过在ASP.NET MVC不同操作的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个MVC ASPNET程序,我想一些变量可以共享给每一位参观者。结果
我输入一些code到Global.asax中:

i created a aspnet mvc program, and i want some variables can be shared to every visitor.
i typed some code into Global.asax:

protected void Application_Start(){ 
    SMEQueue[] SMEtime = new SMEQueue[10];
    Application["waittime"] = SMEtime;
...

不过,我不能修改应用程序的阵列。我可以在每Contyoller操作这样的阅读:

however, i can't modify the array in Application. i can read it in every Actions in Contyoller like this:

SMEQueue[] arr = System.Web.HttpContext.Current.Application["waittime"] as SMEQueue[];

但之后,我在行动改变数组中的值,我所得到的其他行动仍然是在Global.asax中分配原始数组。改动不能传递给其他操作。结果
为什么,我可以做什么?结果
我发现有计算器上很多类似的问题,遗憾的是他们的答案没有为我的工作。

but after i change the values in the array in a Action, what i get in other Actions is still the original array assigned in Global.asax. the modification cannot pass to other Actions.
why and what can i do?
i found there are many similar questions on stackoverflow, sadly their answers do not work well for mine.

更多codeS:结果我在行动编辑变量:

more codes:
i edit the variable in a Action:

public ActionResult Create()
    {
        SMEQueue[] arr = System.Web.HttpContext.Current.Application["waittime"] as SMEQueue[];
        arr[0] = new SMEQueue("hello");//elements of arr are null before

然后我从另一个Action访问:

then i access it from another Action:

public ActionResult TryConnect()
    {
        SMEQueue[] arr = System.Web.HttpContext.Current.Application["waittime"] as SMEQueue[];
        Request.Write(arr[0].ToString());//it' null.


如果我给你一定的价值在的Application_Start一个变量(),我可以在任何地方访问值(任何操作)。但经过我的任何行动修改这个值,我在其他操作读仍然在的Application_Start()。


if i assign some value to a variable in Application_Start(), i can access the value anywhere (any Actions). but after i modify the value in any Action, what i read in other Actions is still the value assigned in Application_Start().

推荐答案

您首先需要了解该应用程序对象通常创建不止一次。因此,不同的请求很可能使用不同的应用对象。因此,它是有道理的,一个动作可能是读你的初始化值,而不是修改后的值,因为它们正在被不同的应用对象处理。

You first need to understand that Application objects are often created more than once. Therefore different requests may well be using different Application objects. So it makes sense that one Action could be reading your start up values rather than your modified values because they are being handled by different Application objects.

应用程序状态的缺点:

的应用范围的应用状态的范围,也可以是
  坏处。存储在应用程序状态变量是全局只
  特定工艺应用程序正在运行中,并且每个
  应用程序可以有不同的值。因此,你不能
  依靠应用程序状态保存唯一值或更新全局
  专柜的Web花园和Web农场的服务器配置。

Application scope The scope of application state can also be a disadvantage. Variables stored in application state are global only to the particular process the application is running in, and each application process can have different values. Therefore, you cannot rely on application state to store unique values or update global counters in Web-garden and Web-farm server configurations.

ASP.NET状态管理建议

一个经典的例子,你可能会遇到的是IIS应用程序蜿蜒而下,由于活动是由缺省为20分钟,我相信。当下一个请求到达服务器的新工作进程将被剥离了一个新的应用程序对象,因此状态。

A classic example you will likely encounter is IIS winding down your application due to inactivity which is by default 20 minutes i believe. When the next request hits the server a new worker process will be spun up with a new Application object and therefore state.

所以基本上,如果你需要保持状态超出单个会话和应用程序,你需要某种形式的国家持续性的,通常的数据库,你写经历了太多/读取。

So basically if you need to maintain state beyond a single session and Application, you need some form of state persistence, typically a database, that you write/read through too.

我会使用:


  • 的HttpContext为每位用户请求存储短命

  • 长期饼干每用户多请求存储住

  • 缓存为长期居住的多用户,多请求数据

  • 对于没有太多(每天,每周,每月)铭记你必须管理风倒的全球查找数据应用程序状态,从旧旋转起来,以新的数据。

  • 最后将深入到数据库或事情的类型其他一些有效的读/写中你似乎在试图做/页计数器等。

要测试这一切只是把一个断点在你的Application_Start code,看看它是如何被多次击中,每次它这是比赛中的一个新的应用程序对象时....再加上在看在你的线程VS的调试窗口可以翔实。

To test all of this simply put a breakpoint on your Application_Start code and see how many times it gets hit, every time it does that's a new Application object in play.... plus looking at your threads in the Debug Windows of VS can be informative.

这篇关于为什么我的HttpContext.Application变量无法通过在ASP.NET MVC不同操作的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:56