I'm trying to access the Application State from a utility class in the web application but I'm not sure of the best way to do it. I looked for a static member but there is none of use (I was thinking somewhere along the lines of HttpContext.Current).
The best solution I found so far is to have a member in the utility class that will be initialized in Application_Start event of the Global.asax.cs (I can get it from this.Application there) but would there be any risk of "something" happening to that reference (I don't care about the application being restarted as I'm only looking for a cache functionality)?
I think I got it!
HttpContext.Current.Application
There is no way to access Application state from outside the HTTTPContext by design. Using Application_Start is the preferred way to initialize "global" values. You can also use the Cache
class for the same purpose. Cache
offers member expiration features that can be useful for data that changes due to events such as a file or database value changing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With