简体   繁体   English

为什么在ASP.NET中经过一段时间后,Global.asax的命中计数器(应用程序变量)设置为“ 0”?

[英]Why Does Hit Counter(Application Variable) from Global.asax set to '0' after some time in ASP.NET?

I have about 50 application variables for each search database. 每个搜索数据库都有大约50个应用程序变量。 In total 50 searchDB which are queried from single Search.aspx page, depending upon the query string passed in the URL it connects to Specific DB. 从单个Search.aspx页中查询的总数为50个searchDB,具体取决于它连接到特定数据库的URL中传递的查询字符串。

Eg: if Search.aspx?li=1 then connect to 1SearchDB, if Search.aspx?li=2 then Connect to 2SearchDB, .....50SearcgDB. 例如:如果Search.aspx?li = 1,则连接到1SearchDB,如果Search.aspx?li = 2,则连接到2SearchDB,..... 50SearcgDB。

I am maintaining the total visitors to each searchDB depending upon the QueryString in URL and incrementing the Application Variable that are in GLOBAL.ASAX file. 我将根据URL中的QueryString维护每个searchDB的总访问者,并增加GLOBAL.ASAX文件中的应用程序变量。

In Global.asax: 在Global.asax中:

void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup
        Application["1"] = 0;          
        Application["2"] = 0;
        .
        .
        Application["50"] = 0;

    }

In Page_Load of Search.aspx.cs: 在Search.aspx.cs的Page_Load中:

int LocalBody = Convert.ToInt32(Request.QueryString["li"]);
public void Page_Load(object sender, EventArgs e)
    {
    Label1.Text = GetHits(LocalBody).ToString();
    }

 private int GetHits(int LocalBody)
    {
        int counter=0;
        switch (LocalBody)
        {
            case 1:
                Application["1"] = (int)Application["1"] + 1;
                counter=(int)Application["1"];
                break;
            case 2:
                 Application["2"] = (int)Application["2"] + 1;
                counter=(int)Application["2"];
                break;
            .
            .
            case 50:
                Application["50"] = (int)Application["50"] + 1;
                counter=(int)Application["50"];
                break;            default:
                break;
        }
        return counter;

    }

Now the problem is when I am starting the application, after some time (30 to 40 mins) it restarts the counter. 现在的问题是,当我启动应用程序时,经过一段时间(30至40分钟),它会重新启动计数器。 When I am using it then it works fine! 当我使用它时,它工作正常! Why does this happen? 为什么会这样?

这是因为在Application_Start程序重新启动时会触发Application_Start并重置您的计数器,并且可能通过您的池设置自动重新启动应用程序。

  1. Application may restart even web.config gets modified 即使修改了web.config,应用程序也可能重新启动
  2. OR any fatal exception occurred - application may restart...so Verify whether any fatal exception being raised... 或发生任何致命异常-应用程序可能会重新启动...所以请验证是否引发了任何致命异常...
  3. I suspect the application counter increments must be sorrounded by Application.Lock(), inorder to synchronize values. 我怀疑应用程序计数器的增量必须与Application.Lock()关联,以便同步值。

hope it helps HydTechie 希望它对HydTechie有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM