在 Session_End 期间访问 HttpApplicationState

发布于 2024-07-06 21:30:26 字数 855 浏览 5 评论 0原文

在我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情癖 2024-07-13 21:30:27

您在哪里创建“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.

笑饮青盏花 2024-07-13 21:30:26

您应该能够使用 Session_End 内部的 Application 属性访问 ApplicationState 对象。

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

(必须以不同的答案回复,因为我没有直接发表评论所需的声誉)

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)

踏雪无痕 2024-07-13 21:30:26

您应该能够使用 Session_End 内部的 Session 属性访问 SessionState 对象。

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

该属性以及更多属性来自 Global.asax 的基类

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文