更新时间:2022-03-18 22:29:48
重新启动IIS池时,Application_End事件才会被触发(或其他一些不太相关的情况) ,而不是由于数据库不可用或其他原因导致网页崩溃。在一个Web应用程序中,当任何用户连接并使用它时,该应用程序正在运行,并且它不会因单个用户的问题而关闭。
您***的方法用尝试...抓住
块并使用它们来发送你的电子邮件。唯一的另一种方法是拥有一个单独的应用程序,可以频繁地轮询数据库和服务 - 这是一个坏主意,除非这是一个安全关键系统(即便如此,你可能希望在设计方面处理的事情比你的问题暗示你有!)
The Application_End event only ever gets fired when the IIS pool is restarted (or a couple of other less relevant cases), not when your web page crashes due to unavailable DB or whatever. In a web app, the app is running while any user is connected and using it, and it doesn't get shutdown for a single user's problem.
Your best approach is to pepper your website withtry...catch
blocks and use those to send your emails. The only other approach is to have a separate app that polls the DB and service at frequent intervals - and that's a bad idea unless this is a safety critical system (and even then, you probably want to handle things rather better in terms of design than your question implies you have!)