简体   繁体   中英

Stop ASP.NET MVC 2 REST svc gracefully on error

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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