views:

930

answers:

3

In my ASP.NET application using InProc sessions, Session_End calls a static method in another object to do session-specific clean up. This clean up uses a shared database connection that I am storing in application state.

The problem is that I cannot see how to access the application state without passing it (or rather the database connection) as a parameter to the clean up method. Since I am not in a request I have no current HttpContext, and I cannot find any other static method to access the state.

Am I missing something?

UPDATE: It appears that my question needs further clarification, so let me try the following code sample. What I want to be able to do is:

// in Global.asax
void Session_End(object sender, EventArgs e) 
{
    NeedsCleanup nc = Session["NeedsCleanup"] as NeedsCleanup;
    nc.CleanUp();
}

But the problem is that the CleanUp method in turn needs information that is stored in application state. I am already doing the following, but it is exactly what I was hoping to avoid; this is what I meant by "...without passing it... as a parameter to the clean up method" above.

// in Global.asax
void Session_End(object sender, EventArgs e) 
{
    NeedsCleanup nc = Session["NeedsCleanup"] as NeedsCleanup;
    nc.CleanUp(this.Application);
}

I just do not like the idea that Global.asax has to know where the NeedsCleanup object gets its information. That sort of thing that makes more sense as self-contained within the class.

+1  A: 

You should be able to access the SessionState object using the Session property from inside Session_End.

void Session_End(object sender, EventArgs e) 
{
    HttpSessionState session = this.Session;
}

This property and a lot more come from the base class of Global.asax

Greg Dean
The Session property is where I get me object that does the clean up. It is the called object that needs access to the _application_ state. And _that_ is where the trouble lies.
DocMax
+2  A: 

You should be able to access the ApplicationState object using the Application property from inside Session_End.

void Session_End(object sender, EventArgs e) 
{
     HttpApplicationState state = this.Application;
}

(had to reply in a different answer because I don't have the reputation needed to comment directly)

Greg Dean
This should get you the application object correctly
Tom
A: 

Where are you creating the "NeedsCleanup" instances? If it's in Session_Start, it makes sense that your global class would know how/when to both create and destroy these instance.

I understand you'd like to decouple the cleanup of NeedsCleanup from its caller. Perhaps a cleaner way would to pass in the "HttpApplication" instance found on both "HttpContext.Current.ApplicationInstance" as well as from your Global class via the "this" reference. Alternatively you could specify any of the aforementioned instance on construction as well, that would a make cleanup less coupled.

Greg Dean
After more thought, I decided you hit the issue with this answer. It is not that my question has no (obvious) solution, but that I am trying to solve the wrong issue. Since "NeedsCleanup" is created when I *can* get the Application state, it should cache the state in its ctor. Nicely decoupled.
DocMax