在 Session_End 期间访问 HttpApplicationState
在我使用 InProc 会话的 ASP.NET 应用程序中,Session_End 调用另一个对象中的静态方法来执行特定于会话的清理。 此清理使用我存储在应用程序状态中的共享数据库连接。
问题是,如果不将应用程序状态(或更确切地说数据库连接)作为参数传递给清理方法,我无法了解如何访问应用程序状态。 由于我不在请求中,所以我没有当前的 HttpContext,并且我找不到任何其他静态方法来访问状态。
我错过了什么吗?
更新:看来我的问题需要进一步澄清,所以让我尝试以下代码示例。 我想要做的是:
// in Global.asax
void Session_End(object sender, EventArgs e)
{
NeedsCleanup nc = Session["NeedsCleanup"] as NeedsCleanup;
nc.CleanUp();
}
但问题是 CleanUp 方法又需要存储在应用程序状态中的信息。 我已经在做以下事情,但这正是我希望避免的; 这就是我上面所说的“...不传递它...作为清理方法的参数”的意思。
// in Global.asax
void Session_End(object sender, EventArgs e)
{
NeedsCleanup nc = Session["NeedsCleanup"] as NeedsCleanup;
nc.CleanUp(this.Application);
}
我只是不喜欢 Global.asax 必须知道 NeedsCleanup 对象从哪里获取其信息。 这类事情在课堂上自成一体更有意义。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在哪里创建“NeedsCleanup”实例? 如果它在 Session_Start 中,那么您的全局类将知道如何/何时创建和销毁这些实例是有意义的。
我了解您希望将 NeedsCleanup 的清理与其调用者分离。 也许一种更简洁的方法是通过“this”引用传递在“HttpContext.Current.ApplicationInstance”上以及从全局类中找到的“HttpApplication”实例。 或者,您也可以在构造时指定任何上述实例,这将使清理工作的耦合度降低。
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.
您应该能够使用 Session_End 内部的 Application 属性访问 ApplicationState 对象。
(必须以不同的答案回复,因为我没有直接发表评论所需的声誉)
You should be able to access the ApplicationState object using the Application property from inside Session_End.
(had to reply in a different answer because I don't have the reputation needed to comment directly)
您应该能够使用 Session_End 内部的 Session 属性访问 SessionState 对象。
该属性以及更多属性来自 Global.asax 的基类
You should be able to access the SessionState object using the Session property from inside Session_End.
This property and a lot more come from the base class of Global.asax