ASP.NET Application state vs a Static object
Asked Answered
L

1

41

if i have a standard ASP.NET application, is there any difference between making an object static as opposed to putting the object instance in the Application state?

from my understanding, both objects exist ONCE for the app domain.

Secondly, what happens if you have a static object in a referenced dll, for an ASP.NET site. It's also part of the app domain, so it will always exist once?

Luckin answered 19/11, 2008 at 23:0 Comment(1)
I don't know details but I would ask yourself a question "is it part of my application state" - then put it there. If it is something static that could also happen in, for instance, windows client - would create it as static.Rights
A
58

From: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q312607

ASP.NET includes application state primarily for compatibility with classic ASP so that it is easier to migrate existing applications to ASP.NET. It is recommended that you store data in static members of the application class instead of in the Application object. This increases performance because you can access a static variable faster than you can access an item in the Application dictionary.

Also, yes, static variables behave the same way regardless of where they are loaded from, and exist exactly once per app domain (unless you're talking about those labeled [ThreadStatic])

Amortizement answered 19/11, 2008 at 23:23 Comment(2)
You also gain type safety when you use static variable instead of the application state object.Bathetic
Newer URL: support.microsoft.com/en-us/help/312607/…Annabal

© 2022 - 2024 — McMap. All rights reserved.