使用而不是ASP.NET应用程序状态的静态变量 [英] Using static variables instead of Application state in ASP.NET

查看:130
本文介绍了使用而不是ASP.NET应用程序状态的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我飞机上用的,而不是在ASP.NET应用程序状态静态变量,我想知道如果这是正确的做法:

  [Global.asax.cs中]...公共类全球:System.Web.HttpApplication
{
    无效的Application_Start(对象发件人,EventArgs的发送)
    {
        //上的应用程序启动时运行code    }    ...    私有静态字典<字符串对象> cacheItems =新词典<字符串对象>();
    私人静态对象更衣室=新的对象();    公共静态字典<字符串对象> CacheItems
    {
        得到
        {
            锁(柜)
            {
                返回cacheItems;
            }
        }        组
        {
            锁(柜)
            {
                cacheItems =价值;
            }
        }
    }    公共静态无效RemoveCacheItem(字符串键)
    {
        cacheItems.Remove(键);
    }    ...
}

正如你可以看到我用自动创建的Global.asax(和code后面)文件。我已经添加了一些静态的变量和方法。我能以这种方式后,使用它们:

  [一些cs文件]
的foreach(KeyValuePair<字符串对象> dictItem在Global.CacheItems)
{
    ...

这是正确的方式,或者我应该代替现有的全球创造新的类?如果我要创建新类我怎么能做到这一点,在哪里呢?


解决方案

微软说什么


  

ASP.NET包括应用程序状态主要是为了与兼容性
  传统的ASP,使得它更容易迁移现有的应用程序,以
  ASP.NET。建议您在存储静态数据成员
  应用程序类,而不是在应用程序对象。这个
  提高性能,因为你可以更快的访问静态变量
  比你可以在应用程序字典访问的项目。


引用:<一href=\"http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607\">http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607

我的expirience

<击>静态变量和应用程序状态之间的主要不同的是,应用程序的状态是所有线程和游泳池一样,但静态唯一的每个池是一样的。

在新的测试中,我看到,应用程序状态变量是一样的静态变量,他们只是引用应用程序一个静态变量,他们只是存在兼容性的原因,因为微软称

如果你有4个游泳池运行您的网站(Web园),那么你有4套不同的静态存储器。

您code

关于您的code,你有您尝试访问您的dictionarry数据的方式错误,你将要在现实的网络错误。在code,这部分锁定全字典的变量,但不锁定你去,当你用它来做出改变。

  //这是不够的,操纵您的数据!
 公共静态字典&LT;字符串对象&gt; CacheItems
    {
        获得{锁(柜){返回cacheItems; }}
        集合{锁(柜){cacheItems =值;}}
    }

正确的做法是锁定附加的所有操作/删除,直到你完成,例如

 私有静态字典&LT;字符串对象&gt; cacheItems =新词典&LT;字符串对象&gt;();
私人静态对象更衣室=新的对象();
公共字典&LT;字符串对象&gt; CacheItems
    {
        {返回cacheItems; }
        集合{cacheItems =值;}
    }SomeFunction()
{
  ...
  锁(柜)
  {
    CacheItems [变量名] = SomeObject;
  }
  ...
}

这是当你在操作应​​用程序状态的数据,你需要使用<击>全球的它锁另一方面 Application.Lock(); Application.UnLock(); 例如:

  Application.Lock();
应用[PageRequestCount] =((int)的应用[PageRequestCount])+ 1;
Application.UnLock();

要关闭与结果:

避免应用程序的状态,只是在你的code使用静态变量。

I plane to use static variables instead of Application state in ASP.NET and am wondering if this is correct approach:

[Global.asax.cs]

...

public class Global : System.Web.HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

    }

    ...

    private static Dictionary<string, object> cacheItems = new Dictionary<string, object>();
    private static object locker = new object();

    public static Dictionary<string, object> CacheItems
    {
        get
        {
            lock (locker)
            {
                return cacheItems;
            }
        }

        set
        {
            lock (locker)
            {
                cacheItems = value;
            }
        }
    }

    public static void RemoveCacheItem(string key)
    {
        cacheItems.Remove(key);
    }

    ...
}

As you can see I use automatically created Global.asax (and code behind) file. I've added some static variables and methods. I can use them after in this manner:

[some .cs file]
foreach(KeyValuePair<string, object> dictItem in Global.CacheItems)
{
    ...

Is this the right way or I should create new class instead of existing Global? If I should create new class how can I do that and where?

解决方案

What Microsoft says

ASP.NET includes application state primarily for compatibility with classic ASP so that it is easier to migrate existing applications to ASP.NET. It is recommended that you store data in static members of the application class instead of in the Application object. This increases performance because you can access a static variable faster than you can access an item in the Application dictionary.

reference : http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607

My expirience

The main different between static variables and Application state, is that the Application state is the same across all threads and pools, but the static is the same only per pool.

After new tests I see that the Application state variables are the same as static variables, and they just reference to a static variable on the application, and they just exist for compatibility reasons as microsoft says

If you have 4 pools running your site (web garden) then you have 4 sets of different static memory.

Your code

About your code, you have bug for the way you try to access your dictionarry data, and you will going to have errors in real web. This part of the code is lock the variable of the full Dictionary but not lock the change you going to make when you use it.

 // this is not enough to manipulate your data !
 public static Dictionary<string, object> CacheItems
    {
        get{ lock (locker){return cacheItems; }   }    
        set{ lock (locker){cacheItems = value;}  }
    }

The correct approach is to lock all actions of add/remove until you done, for example.

private static Dictionary<string, object> cacheItems = new Dictionary<string, object>();
private static object locker = new object();
public Dictionary<string, object> CacheItems
    {
        get{ return cacheItems; }   
        set{ cacheItems = value;}  
    }

SomeFunction()
{
  ...
  lock(locker)
  {
    CacheItems["VariableName"] = SomeObject;
  }
  ...
}

From the other hand when you manipulate data on application state you need to use the global lock of it Application.Lock(); and Application.UnLock(); for example

Application.Lock();
Application["PageRequestCount"] = ((int)Application["PageRequestCount"])+1;
Application.UnLock();

To close with a result:

Avoid Application state and just use static variables on your code.

这篇关于使用而不是ASP.NET应用程序状态的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆