关于asp.net中的Session_End和Application_End方法

时间:2011-04-16 14:24:24

标签: asp.net application-end sessionend

我正在尝试实现一个实现System.Web.HttpApplication接口的类(让我们称之为CustomerConnection类)。我尝试以这种方式在这个类中实现两个事件session_end和application_end事件:

public class CustomerConnection: System.Web.HttpApplication
{


    protected void Session_End(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext context = application.Context;
        SqlConnection connection = context.Items[Database_Con] as SqlConnection;
        connection.close();

    }

    protected void Application_End(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext context = application.Context;
        //someotherEvent
    }

但似乎这些事件不会因应用程序结束或会话注销而被执行。

我应该如何让这些事件被执行?是否我必须通知某些其他事件?

我只是在创建一个新的客户类对象。是否足以让这些事件得以执行或者我的方法从根本上是错误的?

请帮帮我

感谢您的期待

2 个答案:

答案 0 :(得分:2)

你需要在global.asax中使用它:

<%@ Application Language="C#" Inherits="CustomerConnection"%>

警告,我从未尝试过这个,我在这里找到了它:http://www.codedigest.com/Articles/ASPNET/34_Adding_CodeBehind_for_Gloabalasaz_x_file_in_aspnet_20.aspx

答案 1 :(得分:0)

您创建的对象未连接任何内容,因此无法从应用程序接收任何事件。您必须将事件处理程序放在用于Web应用程序的对象中,即在Global.asax.cs文件中。

此外,没有Session_End事件,它是Session_OnEnd