Reputation: 12164
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.
Upvotes: 0
Views: 2765
Reputation: 30057
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.
Upvotes: 0
Reputation: 30057
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)
Upvotes: 3
Reputation: 30057
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
Upvotes: 1