Pretty much what the title describes.
It appears that the Service does wait for IHostedService implementations to finish before reporting the status as stopped, but NOT for IDisposable (or IASyncDisposable) singletons.
This has an awkward side-effect when Restarting the WindowsService as the previous process singleton instances may still be around when new singletons are instantiated in the newly launched process.
Expected Behavior
I would have expected that the service would wait until all singletons are disposed (within some timeout-limit) before reporting the Service as Stopped.
build the solution -> resulting in the executable binary Lastly.exe
register the exe as a windows service using sc.exe create Lastly binPath= {path-to-lastly.exe}
install seq for view aggregated logs. or monitor the log***.txt files in the executable directory.
start the service (via TaskManager)
hit the webapi endpoint by visiting http://localhost:5000/ping.
Restart the window service (via TaskManager)
view logs
The aggregated logs in Seq show the service reporting as stopped before the Ping singleton is disposed.
Additionally it can be seen that a new service instance is launched before the singleton from the previous instance has been disposed.
Note that the hosted service is appropriately 'cleaned up' before the service reports as stopped.
Is there an existing issue for this?
Describe the bug
Pretty much what the title describes. It appears that the Service does wait for
IHostedService
implementations to finish before reporting the status as stopped, but NOT forIDisposable
(orIASyncDisposable
) singletons.This has an awkward side-effect when Restarting the WindowsService as the previous process singleton instances may still be around when new singletons are instantiated in the newly launched process.
Expected Behavior
I would have expected that the service would wait until all singletons are disposed (within some timeout-limit) before reporting the Service as Stopped.
Steps To Reproduce
The repository at https://github.com/lucas-natraj/dueling-services represents a minimal setup to reproduce the issue.
Lastly.exe
sc.exe create Lastly binPath= {path-to-lastly.exe}
log***.txt
files in the executable directory.http://localhost:5000/ping
.Restart
the window service (via TaskManager)The aggregated logs in
Seq
show the service reporting as stopped before thePing
singleton is disposed. Additionally it can be seen that a new service instance is launched before the singleton from the previous instance has been disposed. Note that thehosted
service is appropriately 'cleaned up' before the service reports as stopped.Exceptions (if any)
No response
.NET Version
6.0.100
Anything else?
dotnet --info