I'm currently trying to work out how best to run a .NET Core 2.x application as a Windows Service, specifically to run an NServiceBus endpoint for a messaging system. My original prototype was built from some of the Windows Service hosting docs from Particular. I then took that functioning prototype and turned it into a .NET Standard library for other team members to build from, but found that it wasn't super intuitive for them (or myself if I looked away from it for 2 weeks).
Naturally, after I built a functioning prototype and it was deployed to production, I found a more elegant solution that uses .NET Core's GenericHostBuilder
, courtesy of a Mr. Steve Gordon. The majority of the code makes sense to me, but I'm hung up on the new Thread(...).Start();
, likely because I don't have any specific experience with using Thread
in C# and .NET.
new
ing up aThread
and maintaining no reference to it feels really wrong. I'm concerned that doing that might lead to a memory leak or the Garbage Collector will pick it up? I did find this SO answer which gives me some reassurance that even if I don't hold a reference to theThread
, the CLR will. So it sounds like there shouldn't be any concern of the GC finalizing the thread, right?- Could anyone explain to me why
Abort()
is not called for theThread
? Is it because the CLR manages threads and knows to stop other threads when the main thread shuts down? Or is does it have something to do with calling theServiceBase.Stop()
method in theIHostLifetime.StopAsync()
method?
If these things can be explained in some documentation of Thread
somewhere, I'm more than happy to get an "RTFM" and find the docs. I just haven't found anything that's given me a clear explanation at this point.