C# global.asax 中的 Application_Start() 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1794349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Application_Start() event in global.asax
提问by ACP
Hai guys, My website has thousands of users... I have implemented a background task of sending mails to every user once a day ... I followed this link to do this...
嘿伙计们,我的网站有成千上万的用户......我已经实现了每天向每个用户发送一次邮件的后台任务......我按照这个链接来做到这一点......
http://www.codeproject.com/KB/aspnet/ASPNETService.aspx
http://www.codeproject.com/KB/aspnet/ASPNETService.aspx
My question is will Application_Start() will be fired for every user hitting my website... If so every user will be receiving a n number of mails daily so i want to avoid it...
我的问题是 Application_Start() 是否会为每个访问我网站的用户触发...如果是这样,每个用户每天都会收到大量邮件,所以我想避免它...
采纳答案by Zeus
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.
Application_Start 和 Application_End 方法是不代表 HttpApplication 事件的特殊方法。ASP.NET 在应用程序域的生命周期内调用它们一次,而不是为每个 HttpApplication 实例调用它们。
So When first user will open the site it will hit the application_start method after that it will not.
因此,当第一个用户打开该站点时,它会在之后点击 application_start 方法,然后就不会了。
I will add that what you are trying to do is risky. If you want to do batch email sending then you may want to think about Scheduler which can send emails daily.
我要补充的是,您尝试做的事情是有风险的。如果您想批量发送电子邮件,那么您可能需要考虑可以每天发送电子邮件的 Scheduler。
回答by John Boker
Application_Start only runs when the first person goes to the site and when the app_pool refreshes.
Application_Start 仅在第一个人访问站点和 app_pool 刷新时运行。
It does not happen on every hit to the site.
它不会在每次点击网站时发生。
you might want to think of something like http://www.webcron.org/for a cron-like system to schedule tasks.
您可能需要考虑类似http://www.webcron.org/的类似 cron 的系统来安排任务。
回答by jerjer
It should be on the Application_BeginRequest plus a DB storing the state for every visitor. But the most appropriate is on Scheduler.
它应该在 Application_BeginRequest 上加上一个存储每个访问者状态的数据库。但是最合适的还是在Scheduler上。
回答by Vitaliy Markitanov
For the full picture - here is list of all events which can be fired in global.asax:
完整的图片 - 这里是可以在 global.asax 中触发的所有事件的列表:
http://aspalliance.com/1114_Understanding_the_Globalasax_file.3
http://aspalliance.com/1114_Understanding_the_Globalasax_file.3