dotnet-hosted-services
.NET hosted services - background work on the generic host
This skill owns the host a long-running task runs inside: how the work is registered, which base type to derive from, what happens when it throws, how it reaches a scoped dependency, how it loops, and how it stops cleanly. It stops at the host boundary. When the work is driven by a broker - a queue consumer, an outbox relay, a saga - the delivery contract, idempotency, and retry policy are the broker-messaging skill's (where installed); this skill only owns the host process those consumers happen to live in. The HTTP service around an in-process background task is the web hub skill's (the ASP.NET Core cross-cutting baseline). The general concurrency mechanics - awaiting without deadlock, cancellation threading, SemaphoreSlim / Interlocked, Channel<T> basics, bounded parallelism - are the csharp language baseline's; what the worker loop adds on top of them is in the trap section below. Floor is .NET 8 / C# 12; anything newer is marked optional.
The two host shapes
There is one hosting model and two ways to enter it. Pick by what the process is.
A standalone worker - no HTTP surface, just background work - is a worker binary. The project uses the Microsoft.NET.Sdk.Worker SDK and Host.CreateApplicationBuilder:
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<IngestWorker>();
var host = builder.Build();
host.Run();
Host.CreateApplicationBuilder (the post-.NET-6 linear builder, preferred over the old CreateDefaultBuilder/callback style) wires configuration, logging, and DI the same way the web host does - the hosted services you register run under the same lifecycle.