Are you tired of handling graceful shutdown manually by listening for key-events? Are you held awake at night by doubts of mishandled exceptions and restarts? Then this is the repository for you!
From time to time we write programs that acts as services, without being HTTP-APIs. Maybe we're reading messages from a queue, streaming events from a pipe or just simply keeping an open connection to something, either way we want a service that stays up when we want it, and shuts down cleanly.
It might be tempting to write such services as simple console applications, and just stick a while(true)
or Console.ReadLine()
in somewhere to keep it running. Sometimes this is sufficient, but is this always the best option?
.NET Core 2.1 introduced .NET Generic Host, and in this repository we'll have a look at some of the ways you can use Generic Host to create more robust services.
Kubernetes can do a lot of heavy lifting when it comes to restarting failing programs and keeping long lived programs running, but it still really want you to handle termination of your program in a clean and efficient manner.
TODO: Currently all you have is the talk outline.
SimpleConsoleService
- Run & CTRL + C. Wait, what about Stop and Dispose?
- SIGTERM. Whoops, now it's broken :-/
SimpleHostedService
- Run & CTRL + C. Works!
- SIGTERM. Works!
HandlingExceptions
- You have to handle exceptions yourself
- A quick look at configuration
AspNetInspiredStartup
- Would you like to have something like Startup from ASP.NET Core?
- Hafslund.Configuration and Hafslund.Telemetry works!
- Host.Generic
HostingEventHubs
- EventHubProcessor needs to be unregistered properly
- Also, a lot of factory stuff to inject everything
More info
- Check out .NET Generic Host
- Check out Fundamentals: hosted services
- Not covered: Starting and stopping services. Background services to ASP.NET Core projects.
"Generic Host will replace Web Host in a future release and act as the primary host API in both HTTP and non-HTTP scenarios."
As stated in the Generic Host documentation, Generic Host is the future of hosting in .NET Core. This means that we can expect that ASP.NET Core will switch over to using Generic Host some time in the future.