teodoran / fsharp-generic-host-examples

A collection of examples on how to use .NET Generic Host with F#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

.NET Generic Host with F#

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!

Why should I care about Generic Host?

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 do or Console.ReadKey() 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.

Doesn't Kubernetes do all that work for me?

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.

You won me over! How do I go about navigating this repository?

TODO: Currently all you have is the talk outline.

Talk outline

ConsoleService

  • Run & Any Key. Works! Ship it!
  • Run & CTRL + C. Wait, what about Stop and Dispose?
  • SIGTERM. Whoops, it's broken :-/

HostedService

  • Run & CTRL + C. Works!
  • SIGTERM. Works!
  • BrokenWorker + BackgroundWorker. Blocks startup.
  • HostedWorker. Better fit for some applications.
  • ExceptionWorker + HostedWorker. Unhandled exceptions shuts down host.

ApiService

  • You can use IHostedService and BackgroundService with ASP.NET Core.
  • Remember that you'll have to handle scopes yourself.

More info

About

A collection of examples on how to use .NET Generic Host with F#

License:MIT License


Languages

Language:F# 100.0%