0

I have about 10 Session variable for storing the File Download Counts of each different 10 Categories wise. I dont know why? but my session variable that is set into Global.asax get RESET automatically.

Since, the Machine is not restarted. Still the Counter of File Downloads get Reset. Any Idea? Plz Suggest me any solution.

In Global.asax:

 void Application_Start(object sender, EventArgs e) 
    {
        Application.Add("MGM",0);
        Application.Add("PC",0);
        Application.Add("NC",0);
        Application.Add("TC",0);
        Application.Add("PGC",0);
    }

    The *shortCode* parameter is name of Global Session from Global.asax file. that i am passing to get the counter and increment accordingly.
In Download.aspx.cs Page:

private int GetCount(string shordCode)
{
    int count=0;
    count = Convert.ToInt32(Application[shortCode]);
    lock (Application[shortCode])
    {
        Application[shortCode] = ++count;
    }

    return count;
}

Shall i store value in textfile and update accordingly after certain count say 500. if yes how to do? Our colleague says that if suppose many users downloading file and if both access the same value from textfile then cuncurency may occurs.I am Confused...!Help Appreciated.

SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143

3 Answers3

1

Please refer to the MSDN page for ASP.NET Application State

Excerpt:

Because application state is stored in server memory, it is lost whenever the application is stopped or restarted. For example, if the Web.config file is changed, the application is restarted and all application state is lost unless application state values have been written to a non-volatile storage medium such as a database.

By default, ASP.NET applications running on IIS will have their application pool shut down during periods of inactivity. I believe the default value for this is 20 minutes. Also by default, applications pools are recycled every 1740 minutes (29 hours).

When this happens you will lose anything in the Application[] collection that you haven't stored in a more permanent location, such as a database.

Both of the above-mentioned values can be modified by right-clicking on the specific application pool in inetmgr and clicking on Advanced Properties to bring up the appropriate window.

Joshua Shearer
  • 1,120
  • 10
  • 23
  • Thanks @Joshua for Replying so what should i do to prevent this situation? shall i write to textfile or sqlserver..? – SHEKHAR SHETE Aug 11 '12 at 08:16
  • Most every site that I've ever seen has stored things of this nature directly to the database. The only instance where I would even consider caching this type of data in the Application State would be if the site received a large amount of traffic and the site was being bottle-necked by the database. Even then, I would persist changes directly to the database and simply update my cached result. As far as my understanding goes, this gives you the best of both worlds. – Joshua Shearer Aug 11 '12 at 08:18
  • ok Fine! Thanks a lot...! i will try to store into database..! – SHEKHAR SHETE Aug 11 '12 at 08:25
0

ok good!, Please try my simple app,four you problem:

       protected void Application_Start(object sender, EventArgs e)
    {
        Application.Add("MGM", 0); 
    }

    protected void Session_Start(object sender, EventArgs e)
    {
        Application.Lock();
        Application["MGM"] = System.Convert.ToInt32(Application["MGM"]) + 1; 
        Application.UnLock();
    }

    protected void Session_End(object sender, EventArgs e)
    {
        Application.Lock();
        Application["MGM"] = System.Convert.ToInt32(Application["MGM"]) - 1;
        Application.UnLock();
    }

    protected void Application_End(object sender, EventArgs e)
    {
        Application.Clear();
    }

and you method i changed:

   private int GetCount(string shordCode)
{
    return Convert.ToInt32(Application[shortCode]); 
}
Elyor
  • 900
  • 1
  • 12
  • 26
  • thanks for replying @eli but this is not correct solution to my problem and i dont understand why you are decrementing the count in SessionEnd(). I am incrementing the count only when the file is downloaded..not satisfied with your suggesstion..! – SHEKHAR SHETE Aug 11 '12 at 08:19
0

Ok,great i answer this Q,and try change your method:

Startup your web app:

protected void Application_Start(object sender, EventArgs e) 
{
    Application.Add("MGM",0);
    Application.Add("PC",0);
    Application.Add("NC",0);
    Application.Add("TC",0);
    Application.Add("PGC",0);
}

and changed your get count method logics:

 private int GetCount(string shordCode)
    {
        //current app instance
        var currentApp = HttpContext.Current.ApplicationInstance.Application;

        //get item count
        var count = Convert.ToInt32(currentApp[shordCode]);

        //locking app for your asking count insrement
        currentApp.Lock();
        currentApp[shordCode] = ++count;

        //unlock app
        currentApp.UnLock();

        return count;
    }
Elyor
  • 900
  • 1
  • 12
  • 26