By Elad Lachmi

2011-04-05 07:32:31 8 Comments

I need to implement several application-level behavior in a project I'm currently working on. There are several things I need to get my head around: 1. Where and how do I define application level variables? 2. What is the lifetime of these variables? or more accuratly, in what scenarios are they discarded? (Application pool recycle? App binaries dropped from memory and recompiled on the next request? etc.) 3. Is the global.asax file a good place to put a session counter, or maybe storing the value to a DB / file is a better way of persisting this kind of data?

Any comments or ideas are welcome. Thank you! -Elad


@LeftyX 2011-04-05 08:00:50

Application-level variables have an application life-time. It means that it the application pool is recycled, they're discarded.
The application pool can be recycled for different reasons. IIS 6/7 can be configures so that the app pool is recycled after a certain amount of time, after a certain number of request or at specified intervals. You set an application variable this way:

Application["DbConfig"] = "my value";

but you have to be aware of the problems you might encounter if you try to set/access in different place. You have to adopt a way to lock the variables when they're updated.

I us the web.config for all the configuration parameters and then I've created my own class which I use to store application fields:

namespace Web.My
    public class Application
        public static string ApplicationStorageFolder
                return (System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~"), "_AppStorage"));

If I need set some fields I do it at the application startup Application_Start If you need to persist infos you can create your own config file (xml or simple text) to store and read values at the application startup and shutdown. You can serialize your class in a XML file so you can ready it, repopulating your properties easily. A db would be fine as well. I would do the same with the session counter.

@Elad Lachmi 2011-04-05 08:30:15

Thank you for the detailed reply! So your saying you wouldn't use an app level variable to store a session counter?

@LeftyX 2011-04-05 09:27:51

No. I wouldn't use it for an application counter cause you can't rely on it since it might be recycled. For a session should be fine.

@Elad Lachmi 2011-04-05 09:34:23

Thank you very much. That was just the info I was looking for.

@LeftyX 2011-04-05 09:44:48

No problem. You can find more infos here if you're interested and remember has a cache object too which is very useful in some situations…

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Lifetime of ASP.NET Static Variable

  • 2012-01-18 23:31:24
  • paul simmons
  • 88534 View
  • 77 Score
  • 3 Answer
  • Tags:   c# static

5 Answered Questions

[SOLVED] Global.asax not firing for Release build

2 Answered Questions

[SOLVED] When to use Application_Start vs Init in Global.asax?

1 Answered Questions

[SOLVED] When does an ASP.NET Website project recompile?

1 Answered Questions

[SOLVED] static variable lifetime and application pool recylcing

1 Answered Questions

Using Global.asax file to load user preferred interface language

2 Answered Questions

[SOLVED] How can I determine why my ASP.NET application is recycling

  • 2010-03-09 22:47:20
  • Josh Yeager
  • 4577 View
  • 7 Score
  • 2 Answer
  • Tags: iis session

1 Answered Questions

Sponsored Content