3

I've used the table at the top of this article as a reference. I have three questions:

1 - Can multiple users (from different physical locations) ever share an HttpApplication instance? If so, does this happen by default?

2 - Can multiple users (from different physical locations) ever share an HttpApplicationState instance? If so, does this happen by default?

3 - Can multiple users of an ASP.NET application ever share a singleton instance or a static variable value? If so, does this happen by default?

Thanks for clearing this up.

smartcaveman
  • 41,281
  • 29
  • 127
  • 212

2 Answers2

6

The answer to all six of your question is yes.

Per-user state should be stored in the Session.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

The HttpApplication contains the state of the Asp.Net application on a per w3svc.exe instance. So, per web server in your application, the application state is stored. If you are using web gardening, there are other concerns with the HttpApplication object.

All users on the same server, on the same thread will share HttpApplication, HttpApplicationState, and all static variable values. When multiple HttpApplication instances are running on a server, concurrent users will not be accessing the same instance of these objects. I do NOT recommend storing values in these objects... it is a much better practice to use the Cache object to store values you wish to share across users on a server.

Cache Object

The Cache object can take dependencies and expiration time values... this will allow you to control the 'freshness' of the values you are storing on a server.

Per user values should be stored in the Session object.

Session Object

Jeff Fritz
  • 9,821
  • 7
  • 42
  • 52
  • By "all users on the same server", do you mean all users accessing the same server? Also, where can I find documentation about w3svc.exe? – smartcaveman Mar 06 '11 at 17:31
  • If you are using a webfarm environment, you may have some users on 1 webserver and some users on another. The HttpApplication does NOT communicate across machines. – Jeff Fritz Mar 06 '11 at 17:33
  • 1
    +1, thank you for your answer. It is very helpful, but I feel that SLaks answered the actual question most directly and concisely. – smartcaveman Mar 06 '11 at 17:44
  • 1
    np.. just wanted to make sure that you had some context for what other options were available when attempting to store and access variables between user sessions in Asp.Net – Jeff Fritz Mar 06 '11 at 17:49
  • 1
    @JeffFritz Is HttpApplication state maintained when pooling on the same server? Because I read that ASP.NET actually manages a pool of HttpApplication objects to serve requests. Here: http://msdn.microsoft.com/en-us/library/a0xez8f2%28v=vs.71%29.aspx – Ghasan غسان Aug 29 '14 at 17:44
  • 1
    Yes @Ghasan, you care correct that multiple HttpApplications can be created by IIS. It creates as many as it needs to manage concurrent requests to the server, and this number can be in the range of 1 - 100. More information about HttpApplication instances can e found at: http://support.microsoft.com/default.aspx?scid=kb;en-us;312607 – Jeff Fritz Sep 01 '14 at 18:45
  • I had been using HttpContext.Current.Application but it looks like .Cache would be better. One question though how do I clear the entire cache forcibly? – lathomas64 Oct 28 '15 at 16:35
  • Apparently http://stackoverflow.com/a/16532197/387191 is the way to go. Sorry for double comment i did not see a way to edit my first one. – lathomas64 Oct 28 '15 at 17:33