出现错误时正常停止 ASP.NET MVC 2 REST svc

发布于 2024-11-05 05:43:02 字数 227 浏览 0 评论 0原文

我创建了一个 REST Web 服务,该服务在 Application_Start 上订阅另一个向订阅者发布消息的服务。现在,如果其中一个订阅以不可恢复的方式失败,我会记录错误并抛出消息以允许框架处理异常。我真的更愿意更优雅地处理此消息。出于某种原因,我不知道自己需要做什么。有没有办法捕获我调用订阅管理代码的 Application_Start 事件中的异常,然后停止应用程序。我想我知道答案,只是不记得该怎么做。

蒂亚!

I have created a REST web service that, on Application_Start, subscribes to another service that posts messages to subscribers. Right now, if one of those subscriptions fails in a non-recoverable way, I log the error and throw the message to allow the framework to handle the exception. I would really prefer to handle this message more gracefully. For some reason, I am at a loss as to what I need to do. Is there a way to catch the exception in the Application_Start event where I call my subscription management code, and then stop the application. I think I know the answer just can't recall how to do so.

TIA!

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

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

发布评论

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

评论(1

水溶 2024-11-12 05:43:02

我会在可能失败的部分周围放置一个 try/catch,并且在 catch 中,您可以卸载应用程序域,如所述

我认为您还可以创建一个自定义错误页面来向用户显示,这看起来比默认服务器错误页面。

I would put a try/catch around the part that could fail, and in the catch, you could either unload the app domain, as noted here, which would cause the next hit to re-run Application_Start, or you could simply write to a file (or copy into place) named "app_offline.htm" into your application's IIS directory. When that file is present, IIS will automatically display that page instead of your app, so you could make some static "Sorry, we are offline now. Try again later." page to show the users.

I think you could also create a custom error page to show the user, that would look nicer than the default ServerError page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文