Make WindowsServiceLifetime gracefully stop (#83892)
* Make WindowsServiceLifetime gracefully stop
WindowsServiceLifetime was not waiting for ServiceBase to stop the service. As a result
we would sometimes end the process before notifying service control manager that the service
had stopped -- resulting in an error in the eventlog and sometimes a service restart.
We also were permitting multiple calls to Stop to occur - through SCM callbacks, and through
public API. We must not call SetServiceStatus again once the service is marked as stopped.
* Alternate approach to ensuring we only ever set STATE_STOPPED once.
* Avoid calling ServiceBase.Stop on stopped service
I fixed double-calling STATE_STOPPED in ServiceBase, but this fix will
not be present on .NETFramework. Workaround that by avoiding calling
ServiceBase.Stop when the service has already been stopped by SCM.
* Add tests for WindowsServiceLifetime
These tests leverage RemoteExecutor to avoid creating a separate service
assembly.
* Respond to feedback and add more tests.
This better integrates with the RemoteExecutor component as well,
by hooking up the service process and fetching its handle.
This gives us the correct logging and exitcode handling from
RemoteExecutor.
* Honor Cancellation in StopAsync
* Fix bindingRedirects in RemoteExecutor
* Use Async lambdas for service testing
* Fix issue on Win7 where duplicate service descriptions are disallowed
* Respond to feedback
* Fix comment and add timeout