如何处理来自 global.asax 外部的 Application_End 事件

发布于 2024-11-09 10:21:23 字数 179 浏览 0 评论 0原文

我们可以通过名为 Application_End() 的 global.asax 文件创建方法附加到此事件。但我需要像这样附加

HttpContext.ApplicationInstance.ApplicationEnd+=OnApplicationEnd;

它有什么办法吗?

We can attach to this event from global.asax file creating method with name Application_End(). But I need to attach to it like this

HttpContext.ApplicationInstance.ApplicationEnd+=OnApplicationEnd;

Is there any way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

_蜘蛛 2024-11-16 10:21:23

就这样解决了问题。

    public class MyHttpApplication:HttpApplication
    {
        public event Action ApplicationEnd;
        protected void Application_End()
        {
            if (ApplicationEnd != null)
               ApplicationEnd();
        } 
    }

在 global.asax 中定义

<%@ Application Inherits="MyLib.MyHttpApplication" Language="C#" %>

然后在代码中

var app = HttpContext.ApplicationInstance as MyHttpApplication;
app.ApplicationEnd += () => { // do something };

Have solved the problem in such way.

    public class MyHttpApplication:HttpApplication
    {
        public event Action ApplicationEnd;
        protected void Application_End()
        {
            if (ApplicationEnd != null)
               ApplicationEnd();
        } 
    }

In global.asax defined

<%@ Application Inherits="MyLib.MyHttpApplication" Language="C#" %>

Then in code

var app = HttpContext.ApplicationInstance as MyHttpApplication;
app.ApplicationEnd += () => { // do something };
风吹短裙飘 2024-11-16 10:21:23

Application_End 是一个特殊的“事件”,由 Asp.net 调用,不属于 HttpApplication 类。

来自MSDN
*Application_Start 和 Application_End 方法是不代表 HttpApplication 事件的特殊方法。 ASP.NET 在应用程序域的生命周期内调用它们一次,而不是针对每个 HttpApplication 实例。*

我认为您可以将相同的行为附加到 AppDomain.DomainUnload 事件并处理程序

//your global.asax class contrauctor
public GlobalApplication()
        {

            AppDomain.CurrentDomain.DomainUnload += ApplicationEnd;
        }


        private void ApplicationEnd(object sender, EventArgs e)
        {

        }

Application_End is a special "event" that is called by Asp.net that doesn't belog to the HttpApplication class.

From MSDN
*The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance.*

I think you can have the same behaviour attaching and handler to the AppDomain.DomainUnload event

//your global.asax class contrauctor
public GlobalApplication()
        {

            AppDomain.CurrentDomain.DomainUnload += ApplicationEnd;
        }


        private void ApplicationEnd(object sender, EventArgs e)
        {

        }
幽梦紫曦~ 2024-11-16 10:21:23

我知道答案已经给出,但也想包括这样的方式:

[assembly: PreApplicationStartMethod(typeof(Bootstraper), "Start")]
[assembly: ApplicationShutdownMethod(typeof(Bootstraper), "End")]
public static class Bootstraper
{
    public static void End()
    {
        ...
    }
    public static void Start()
    {
        ...
    }
}

I know the answer is already given but wanted to also include this way:

[assembly: PreApplicationStartMethod(typeof(Bootstraper), "Start")]
[assembly: ApplicationShutdownMethod(typeof(Bootstraper), "End")]
public static class Bootstraper
{
    public static void End()
    {
        ...
    }
    public static void Start()
    {
        ...
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文