✅ Hosted Service running as Windows Service
I have an application running as a Windows Service and I'd like to run some code after the system starts and before it shuts down.
I have tried using HostedServices and everything works fine if I manually start and stop the service. However, it does not work when starting or shutting down the system.
Here is a simplified version of my service, I can also provide an
.exe
if necessary.
During StartAsync
I'm executing a network call, but upon reviewing the logs after booting the system, I see this exception:
System.Net.Http.HttpRequestException: No such host is known.
(Presumably a DNS error due to host not being resolved yet.)
Followed by the Application Started event.
Additional details:
As for StopAsync
, I believe the method is not executing as I see no logs at all.12 Replies
I'm thinking
StartAsync
is executing too early while the system is booting up. Maybe I could delay it by polling Dns until it starts resolving or registering an application started event where I connect the websocket client, but it doesn't feel right to me.
Basically, I want to connect the websocket client while the underlying platform is starting the service.
Also, I want to execute some code and dispose the client when the platform restarts or shuts down.echo.websocket.org doesn't look like it's available anymore https://www.lob.com/blog/websocket-org-is-down-here-is-an-alternative so that may explain why you're unable to connect to it in your StartAsync.
echo.websocket.org no longer available
websocket.org shutdown, here are some alternative solutions for developers to echo requests made using websockets
when you say you don't see StopAsync executing, is your app hanging when you send SIGTERM with ctrl-c?
It does work, I'm using this endpoint as a sample but even this sample works fine when I start/stop the service manually. https://websocket.org/tools/websocket-echo-server/
StopAsync executes when I manually stop the service with ctrl-c. It does not seem to execute when restarting the machine
oh I see, apologies for missing that detail. in that case it sounds like an issue with how windows is communicating with the service...
just to check, you're using the .UseWindowsService() method when constructing your app host? (or whatever it's called)
from Extensions.Hosting.WindowsService
No, I'm using AddWindowsService() as described here. https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-8.0&tabs=visual-studio#app-configuration
Host ASP.NET Core in a Windows Service
Learn how to host an ASP.NET Core app in a Windows Service.
yeah, that's what I mean
i assumed that on shutdown Windows gracefully killed services via the same mechanism as manually stopping them
but maybe it's sending the equivalent of SIGKILL if your service takes too long to die
apologies, i won't be of much help beyond this point
I believe it's related to this open issue https://github.com/dotnet/runtime/issues/83093
GitHub
CoreCLR's CNTRL_SHUTDOWN_EVENT handler prevents graceful exit of se...
Description I am not sure if this belongs here, but this is a new weird behaviour I have been fighting with lately. I have a service, where the 'Main_Worker' is a 'IHostedService' a...
always great when your problem ends up being an open issue
my condolences
Update on
StartAsync
while not the most elegant, if I delay the execution, the service works which leads me to believe it's related to the DNS not being resolved at first but I'm not sure how to properly ensure it's running
Not Working
Working
Documenting for further reference:
1. Windows Services do not exit/shutdown gracefully and therefore
StopAsync
is never called. Here is the open issue: https://github.com/dotnet/runtime/issues/83093
2. The reason why StartAsync
runs into a Socket exception during system/platform startup is because the method is executed before the network is available. Here's my workaround:
GitHub
CoreCLR's CNTRL_SHUTDOWN_EVENT handler prevents graceful exit of se...
Description I am not sure if this belongs here, but this is a new weird behaviour I have been fighting with lately. I have a service, where the 'Main_Worker' is a 'IHostedService' a...