ASP.NET HttpApplication.EndRequest事件未被触发

时间:2022-12-02 14:08:27

According this MSDN article HttpApplication.EndRequest can be used to close or dispose of resources. However this event is not fired/called in my application.

根据这篇MSDN文章,HttpApplication.EndRequest可用于关闭或处置资源。但是,在我的应用程序中不会触发/调用此事件。

We are attaching the handler in Page_Load the following way:

我们通过以下方式在Page_Load中附加处理程序:

HttpContext.Current.ApplicationInstance.EndRequest += ApplicationInstance_EndRequest;

The only way is to use the Application_EndRequest handler in Global.asax, but this is not acceptable for us.

唯一的方法是在Global.asax中使用Application_EndRequest处理程序,但这对我们来说是不可接受的。

4 个解决方案

#1


14  

You can use your own HttpModule to capture the EndRequest if you don't want to use the global.asax.

如果您不想使用global.asax,可以使用自己的HttpModule捕获EndRequest。

public class CustomModule : IHttpModule 
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += new EventHandler(context_EndRequest);
    }

    private void context_EndRequest(object sender, EventArgs e)
    {
        HttpContext context = ((HttpApplication)sender).Context;
        // use your contect here
    }
}

You need to add the module to your web.config

您需要将模块添加到web.config中

<httpModules>
    <add name="CustomModule" type="CustomModule"/>
</httpModules>

#2


5  

Per the MSDN documentation, this event occurs AFTER the page is completed, just like BeginRequest. Therefore as far as I know it is not possible to catch this at the page level

根据MSDN文档,此事件在页面完成后发生,就像BeginRequest一样。因此,据我所知,无法在页面级别捕获此信息

#3


2  

I believe a better way to chain this event is by providing a base class to global.asax

我相信链接此事件的更好方法是为global.asax提供基类

Language="C#" Inherits="YourBaseClass"

and then overriding Init():

然后重写Init():

public override void Init()
{
    base.Init();

    BeginRequest += new EventHandler(OnBeginRequest);
    EndRequest += new EventHandler(OnEndRequest);
}

seems to be working for me (catch a breakpoint) but I'm really doing anything with it.

似乎对我有用(抓住一个断点),但我真的在做任何事情。

Since there are multiple HttpApplication instances, setting it within Page_load may keep adding it to a non-specific instance. Oddly, I've also noticed that HttpApplication.Init() does not get called for the very first HttpApplication instance (but Application_start) does.

由于存在多个HttpApplication实例,因此在Page_load中设置它可能会将其添加到非特定实例中。奇怪的是,我也注意到第一个HttpApplication实例(但是Application_start)没有调用HttpApplication.Init()。

#4


1  

The page is probably being disposed before the event fires. You might want to try to do your work in the Page_Unload handler.

该页面可能在事件触发前被处置。您可能希望尝试在Page_Unload处理程序中完成工作。

#1


14  

You can use your own HttpModule to capture the EndRequest if you don't want to use the global.asax.

如果您不想使用global.asax,可以使用自己的HttpModule捕获EndRequest。

public class CustomModule : IHttpModule 
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += new EventHandler(context_EndRequest);
    }

    private void context_EndRequest(object sender, EventArgs e)
    {
        HttpContext context = ((HttpApplication)sender).Context;
        // use your contect here
    }
}

You need to add the module to your web.config

您需要将模块添加到web.config中

<httpModules>
    <add name="CustomModule" type="CustomModule"/>
</httpModules>

#2


5  

Per the MSDN documentation, this event occurs AFTER the page is completed, just like BeginRequest. Therefore as far as I know it is not possible to catch this at the page level

根据MSDN文档,此事件在页面完成后发生,就像BeginRequest一样。因此,据我所知,无法在页面级别捕获此信息

#3


2  

I believe a better way to chain this event is by providing a base class to global.asax

我相信链接此事件的更好方法是为global.asax提供基类

Language="C#" Inherits="YourBaseClass"

and then overriding Init():

然后重写Init():

public override void Init()
{
    base.Init();

    BeginRequest += new EventHandler(OnBeginRequest);
    EndRequest += new EventHandler(OnEndRequest);
}

seems to be working for me (catch a breakpoint) but I'm really doing anything with it.

似乎对我有用(抓住一个断点),但我真的在做任何事情。

Since there are multiple HttpApplication instances, setting it within Page_load may keep adding it to a non-specific instance. Oddly, I've also noticed that HttpApplication.Init() does not get called for the very first HttpApplication instance (but Application_start) does.

由于存在多个HttpApplication实例,因此在Page_load中设置它可能会将其添加到非特定实例中。奇怪的是,我也注意到第一个HttpApplication实例(但是Application_start)没有调用HttpApplication.Init()。

#4


1  

The page is probably being disposed before the event fires. You might want to try to do your work in the Page_Unload handler.

该页面可能在事件触发前被处置。您可能希望尝试在Page_Unload处理程序中完成工作。