ASP.NET中的静态变量(Static variables in ASP.NET)

有一篇文章建议将ASP.NET应用程序状态存储在HttpApplication类的静态成员中(在Global.asax.cs中)。

将应用程序状态存储在其他类的静态成员中怎么样?

我试图这样做,似乎有几个这些变量的实例可以存在(每个AppDomain单个实例?)。 这是真的,我们应该总是只使用应用程序类的静态字段? 或者没关系?

There is an article that recommends to store ASP.NET application state in static members of HttpApplication class (in Global.asax.cs).

What about storing application state in static members of other classes?

I tried to do so and it seems that there are several instances of these variables can exist (single instance per AppDomain?). Is it true and should we always use only Application class's static fields? Or it doesn't matter?

最满意答案

它在许多情况下工作得非常好,比Application更好(例如,它是强类型的)。 只要确保你知道线程和锁定问题。

作为个人经验,我设法在几个网站的static类中缓存ASP.NET应用程序的配置信息。

It works pretty well and it's better than Application in many cases (it's strongly typed, for instance). Just make sure you are aware of threading and locking issues.

As a personal experience, I've managed to cache configuration information for ASP.NET app in a static class in a couple Web sites.

更多推荐