I've been reading through the MS documentation regarding the deployment of a Windows service to run a Worker App. Doc ref --> https://docs.microsoft.com/en-us/dotnet/core/extensions/windows-service
The MS code sample talks about the need to add an Environment.Exit(1); inside the exception handler in order that the Windows Service Management can leverage the configured recovery options.
There are a few concepts that are not clear to me and hoping someone can advise:
In my own project, my background services include various classes and operations such as connection managemenet for Azure IOT Hubs Device Client. In some cases, i simply do no want to force the environment i.e. the whole app to exit on every catch/exception scenario, but the docs are not clear on whether we are expceted to do this? I mean why catch an exception if we're going to simply wipe out the running of the application every time? doesnt make sense to me...
The next point ref the following statement "To correctly allow the service to be restarted, you can call Environment.Exit with a non-zero exit code" but then earlier in the article, it also talks about the two options available for 'BackgroundServiceExceptionBehavior':
- Ignore - Ignore exceptions thrown in BackgroundService. StopHost
- The IHost will be stopped when an unhandled exception is thrown.
An unhandled exception in my mind means the app has gone fowl on something that hasnt been appropriately caught in the right place i.e. where no try/catch block exists. So how does one provision an 'Environment.Exit(1)' to something they havent yet accounted for? And what happens in thie scenario?
The way the article reads to me suggests that the only way we can ensure the Windows Service will manage the re-starting of the app succesfully is from any exception that we knowingly caught, but that equally that doesnt tie up with what the general article is suggesting will happen.
Totally confused :(
