0

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?

Mike G
  • 4,232
  • 9
  • 40
  • 66
SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143

2 Answers2

0

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.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • but i am not restarting the Machine. It must persist still i restart my computer.When i Start application after 20-25mins it shows the Correct Counter that exists during my last execution count+1. – SHEKHAR SHETE Jul 04 '12 at 10:28
  • @SHEKHARSHETE The pool have by default auto-restart settings, if you can open and see it and change them then you can avoid that. – Aristos Jul 04 '12 at 10:29
  • ok @Aristos i will check that..! If i host it on the WebServer in IIS will it work? – SHEKHAR SHETE Jul 04 '12 at 10:34
  • @SHEKHARSHETE the pool is the same, the roster is did not affect it. To make it work as you wish save your values on a database, or on a file, or on xml or something when the application is restart, and read them again back when is starts. – Aristos Jul 04 '12 at 10:42
0
  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

HydPhani
  • 592
  • 6
  • 13