When I create a new service fabric application with a stateless aps.net core service, why is this line of code in the main method of the service?
// prevents this host process from terminating so services keep running
Thread.Sleep(Timeout.Infinite)
Obviously as the code comment above the actual code says, this is to prevent the host process from terminating. But why not make the main method return an async Task
instead of void
to be able to use await Task.Delay()
?
From my understanding the latter would be better because the main thread would be put back into the thread pool to actually do something useful and not just sit there doing nothing other than waiting.
The code a couple of lines above leads me to believe that this is just a leftover from when the main method could not return an async Task
.
ServiceRuntime.RegisterServiceAsync("FirstStatelessApiType",
context => new FirstStatelessApi(context)).GetAwaiter().GetResult();
(GetAwaiter().GetResult()
would also be unnccessary if the method would just return an async Task
as it could then be awaited)
Edit:
I have already tested switching from void Main
to async Task Main
and using await Task.Delay(Timeout.Infinite)
instead of Thread.Sleep
and did not see a difference in the behavior of my service (api endpoints were still running and working as expected)