Improve system integration for Host
This is a spanning issue as we have multiple issuer related to host integration with either windows service or systemd flows. We should reconsider the integration as such rather then try to solve each of these issues separately.
Tagging subscribers to this area: @dotnet/area-extensions-hosting See info in area-owners.md if you want to be subscribed.
Please during this activity reconsider also #77381: it was marked as a duplicated of #50019 but it isn't !!!! Note that this still apply with net 11 preview where the exception is correctly re-throwed on main program thread. But the SCM still reports the service as (gracefully) Stopped because:
-
the SCM is notified of a completed stop https://github.com/dotnet/runtime/blob/4df47f771f0193caeeedc4d8db866c48059c5421/src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs#L378
-
and only after the exceptions are re-throwed https://github.com/dotnet/runtime/blob/4df47f771f0193caeeedc4d8db866c48059c5421/src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs#L415-L430
IMHO to allow a correct behavior with SCM is necessary to inform in some way the WindowsServiceLifetime that something is bad in the Host (i,e, exposing _backgroundServiceExceptions in the IHost ???) so that it could react (i.e. setting an ExitCode before calling SCM Stop or not calling SCM Stop at all in this case)
@beppemarazzi I agree, so I have reopened the issue.