views:

34

answers:

4

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)?

+1  A: 

I think I got it!

HttpContext.Current.Aplication
CyberDude
A: 

Try

System.Web.HttpRuntime
o6tech
+1  A: 

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.

Dave Swersky
Yes, Cache has more functionality, I might use it instead.
CyberDude
A: 

You can simply create a static class member:

public class MyGlobalCache
{
    public static string SomeValue{get;set;}
}

This gets stored at the application level, which means that you get the same functionality of the Application state. The static member will be available across all layers (webpages and non-webpages).

code4life