Asp.net静态对象有时会出现非全球 [英] Asp.net static object appears sometimes as non global

查看:143
本文介绍了Asp.net静态对象有时会出现非全球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序客户端和ASP.Net + ADO.Net GoDaddy的托管服务器上。在asp.net有静态数据结构存储(其中包括)Android的位置,这个数据结构也坚持到SQL Server 2008数据库。每次Android客户端改变位置发送其位置到服务器,更新的数据结构和数据基础。每次一个应用开始的数据结构是根据从数据的基础上的持久数据填充。

中存在的问题:altough数据结构是静态因此应是全球性的,它看起来像有时也有这种静态的数据结构的一个以上的实例

是任何人都知道这个问题的?
我怎样才能prevent这从hapening?


解决方案

  

此对象实际上已经与名作为关键字和信息的字典
  值。这个对象是根据Android客户端的接入更新
  通过HTTP的asp.net thorugh ASPX的Page_Load:
  HttpContext.Current.Application.Lock()。


第一个错误是您使用类型的锁,而不是正确的。

应用程序。锁() Application.Unlock() 被设计成锁机构只对<一个href=\"http://stackoverflow.com/questions/10960695/using-static-variables-instead-of-application-state-in-asp-net/10964038#10964038\">the Application变量的。

要纠正锁定静态字典,你需要使用锁(){} 为:

 私有静态只读对象SYNCLOCK =新的对象();
公共静态字典&LT; INT,INT&GT; DictMem =新词典&LT; INT,INT&GT;();......有些函数内部....
锁定(SYNCLOCK)
{
    //用你的字典行动
    DictMem [2] = 3;
}

另外,我想告诉你,静态不在于你只对你的程序的一个保证。如果提供使用Web园有多个,而当池,然后将回收你失去他们。

I have an android application as client and ASP.Net + ADO.Net hosted on GoDaddy server. The asp.net has static data structure which stores (among other) the android locations, this data structure is persist also to SQL server 2008 data base. each time the android client change position it send its location to the server which update the data structure and data base. each time the applicaton start the data structure is filled according to the persist data from the data base.

The Problems : altough the data structure is static thus should be global it looks like sometimes there are more then one instances of this static data structure.

Is anyone aware of this issue ? How can i prevent this from hapening ?

Nathan

解决方案

This object is has actually a dictionary with name as key and info as value. This object is updated according to android client which access the asp.net via HTTP thorugh aspx Page_Load : HttpContext.Current.Application.Lock() .

The first error is that you use that kind of lock and not the correct one.

The Application.Lock() and Application.Unlock() are designed as lock mechanism only for the Application variables.

To correct lock your static Dictionary you need to use the lock(){} as:

private static readonly object syncLock = new object();    
public static Dictionary<int, int> DictMem = new Dictionary<int, int>();

... inside some function....
lock (syncLock)
{
    // actions with your dictionary
    DictMem[2]=3;
}

Also I like to tell you that the static is not guaranty that you have only one on your program. If the provider use web garden you have more than one, and when the pool recycles then you loose them.

这篇关于Asp.net静态对象有时会出现非全球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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