简体   繁体   English

我的ASP.NET MVC网站中的Application_end方法几乎同时被多次奇怪地调用

[英]The Application_end method in my ASP.NET MVC website is called many times weirdly almost at the same time

It is an ASP.NET MVC website. 这是一个ASP.NET MVC网站。 I add some log in the Application_end method. 我在Application_end方法中添加一些日志。

I know there are some circumstances when the Application_end will be called, such as application pool's recycle, web.config file's change, or bin file's change. 我知道在某些情况下会调用Application_end,例如应用程序池的回收,web.config文件的更改或bin文件的更改。 But my problem here is very weirdly.The Application_end methon in my ASP.NET MVC website is called many times weirdly almost at the same time. 但是我的问题很奇怪.ASP.NET MVC网站中的Application_end methon几乎在同一时间被怪异地多次调用了。

Here is my code: 这是我的代码:

protected void Application_end()
    {
        FileLog.Info("The website is starting to quit...", LogType.Info);
        PersistentService.RunSignal = false;
        //To wait for pesistent action finish
        Task.WaitAll(_tasks.ToArray(), 1000 * 300);
        FileLog.Info("The website finish quit...", LogType.Info);
    }

Here is my log result: 这是我的日志结果: 在此处输入图片说明

You can see, the Application_end is called 4 times in the same second. 您可以看到,Application_end在同一秒内被调用了4次。 I guess, it can be called one time if the application pool is recycled at that time, but why 4 times? 我猜想,如果那时应用程序池被回收了,可以称之为一次,但是为什么要回收4次呢?

Could anyone help? 有人可以帮忙吗? Thanks. 谢谢。

Edit: I notice that my application pool is with 5 worker processes at max. 编辑:我注意到我的应用程序池最多具有5个工作进程。 Is every work process's end will call Application_end method? 是每个工作流程的结束都会调用Application_end方法吗? 在此处输入图片说明

You are using parameter Maximum Worker Process = 5 that means there are up to 5 process serving your application. 您正在使用参数Maximum Worker Process = 5 ,这意味着最多有5个进程为您的应用程序提供服务。 When IIS resets, all processes ends same time. IIS重置后,所有进程将同时结束。

It seems that it's called when the application domain ends, so it might make sense that its called once per process: 似乎在应用程序域结束时调用了它,因此每个进程调用一次可能是有道理的:

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.

From https://msdn.microsoft.com/en-us/library/ms178473.aspx https://msdn.microsoft.com/zh-cn/library/ms178473.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM