SHEKHAR SHETE
SHEKHAR SHETE

Reputation: 6066

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. 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.

Eg: if Search.aspx?li=1 then connect to 1SearchDB, if Search.aspx?li=2 then Connect to 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.

In 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:

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. When I am using it then it works fine! Why does this happen?

Upvotes: 0

Views: 1143

Answers (2)

HydTechie
HydTechie

Reputation: 606

  1. Application may restart even web.config gets modified
  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.

hope it helps HydTechie

Upvotes: 0

Aristos
Aristos

Reputation: 66641

This is because the Application_Start is fired and reset your counters when the application restart, and probably your application auto-restarts by your settings of your pool.

Upvotes: 0

Related Questions