1

I have a WCF service application hosted in IIS. On startup, it reads the SQL connection information from the web.config and connects to the SQL Server to get the necessary information to bootstrap the application. If the SQL Server is unavailable for some reason, we want the service to shutdown - which I currently accomplish by throwing an exception.

The problem we have with this is that, if a service fails to start more than N times, IIS helpfully turns off the application pool - which means that, after the SQL Server connection issue is corrected (possibly entirely external to the IIS server), a sysadmin has to think to go an turn the app pool back on.

What I'd like to do is have the service gracefully exit. I know that there are ways to do this with ASP.Net Core (i.e., ways to hook into the lifecycle management of the process and tell it to exit), but have been unable to find anything like that for .Net Framework/ASP.Net.

Is there such an API that I can use?

David Mullin
  • 688
  • 1
  • 8
  • 21
  • 1
    You want to shut down the service...but you don't want the app pool stopped? I'm confused on what you're trying to accomplish here. – mason Dec 21 '20 at 21:20
  • Currently, a call comes in and IIS starts the service, which fails to connect to the DB and the fails to start. If this is detected fast enough, and the underlying problem (like the SQL Server being off) is fixed, then the next time a call comes in and IIS starts the service, it will start normally. However, if it fails more than N times in a row, IIS will disable the app pool - so even if the SQL Server connectivity problem is fixed, the service won't start until someone manually restarts the app pool. So, I need a way for the service to not start without simply erroring. – David Mullin Dec 21 '20 at 22:09
  • In this case, service = app pool. Therefore if you don't want it to stop trying to start, then you need to [disable IIS rapid fail protection](https://learn.microsoft.com/en-us/iis/configuration/system.applicationhost/applicationpools/add/failure). – mason Dec 22 '20 at 00:01
  • A single app pool can host multiple services, so there isn't a 1-to-1 equivalency there. You seem to be suggesting that it is failing to start the app pool, not the application? – David Mullin Dec 22 '20 at 13:39
  • You should be putting each app in a dedicated app pool, not sharing app pools across apps. See [this question](https://stackoverflow.com/questions/7553088). Note that in the years since that was asked, resources have gotten cheaper and the overhead of an extra w3wp.exe is negligible. – mason Dec 23 '20 at 16:00
  • Yes, I am aware of that. But, are you saying that it is the app pool that is failing to start, and not the application? Is there no way for the application to "gracefully shut down"? Our customers aren't going to like the "disable IIS rapid fail protection" answer. – David Mullin Dec 23 '20 at 16:07
  • 1
    Then stop making your application fail to start. Wrap it in a retry policy so it keeps retrying. Or make it an asynchronous process, and any code that depends on that process to wait until that process is complete before using it. If you don't want to disable rapid fail protection (which is what keeps your app from starting after multiple failures) then you need to make your app not fail to start. You can't have it both ways. – mason Dec 23 '20 at 16:10
  • So, you're saying there is no way for it to gracefully shutdown? No way at all? – David Mullin Dec 23 '20 at 16:40
  • You could potentially reach out to the IIS Management utilities and tell it to shut the app down. But then you're back in the same boat: some sysadmin has to manually launch the app again. So it's really no different than if rapid fail protection had kicked in. – mason Dec 23 '20 at 17:15
  • Would that shut down the application, or the app pool? – David Mullin Dec 23 '20 at 17:17
  • You can [view the documentation yourself](https://learn.microsoft.com/en-us/iis/get-started/getting-started-with-iis/getting-started-with-appcmdexe) for the command line, or the [.NET lib](https://johnlnelson.com/2014/06/15/the-microsoft-web-administration-namespace/). In regards to your question: does it really matter? Someone *still* has to to go start up whatever you've stopped. – mason Dec 23 '20 at 17:22
  • There is a difference. If the app pool is stopped, it has to manually be restarted. If the application is stopped, the next time a call comes in, IIS will automatically start it (i.e., no human interaction required). Thanks for replying to this, BTW - I do appreciate you weighing in. I'll look at that documentation. – David Mullin Dec 23 '20 at 17:43

1 Answers1

1

Turns out that this method is available and works.

https://learn.microsoft.com/en-us/dotnet/api/system.web.hosting.hostingenvironment.initiateshutdown?view=netframework-4.8

David Mullin
  • 688
  • 1
  • 8
  • 21