Switchknife.Hosting
0.1.0
dotnet add package Switchknife.Hosting --version 0.1.0
NuGet\Install-Package Switchknife.Hosting -Version 0.1.0
<PackageReference Include="Switchknife.Hosting" Version="0.1.0" />
<PackageVersion Include="Switchknife.Hosting" Version="0.1.0" />
<PackageReference Include="Switchknife.Hosting" />
paket add Switchknife.Hosting --version 0.1.0
#r "nuget: Switchknife.Hosting, 0.1.0"
#:package Switchknife.Hosting@0.1.0
#addin nuget:?package=Switchknife.Hosting&version=0.1.0
#tool nuget:?package=Switchknife.Hosting&version=0.1.0
Switchknife.Hosting
PollingService, a BackgroundService that does one thing on a timer and never stops the host. Since .NET 6 an exception that leaves ExecuteAsync stops the whole host by default, so a poller that lets one database timeout through takes every endpoint in the process down with it. This base class catches what a poll throws, logs it, backs off and tries again.
public sealed class FeedPoller(FeedClient feeds, IOptions<FeedOptions> options, TimeProvider time, ILogger<FeedPoller> logger)
: PollingService(time, logger)
{
protected override string Name => "feeds";
protected override TimeSpan Interval => options.Value.PollInterval; // read before every wait
protected override Task PollAsync(CancellationToken cancellationToken) => feeds.FetchNewAsync(cancellationToken);
}
builder.Services.AddSingleton(TimeProvider.System);
builder.Services.AddHostedService<FeedPoller>();
What the loop does:
- Polls once on start, then waits. There is no initial delay and no jitter.
- After a poll that returns: calls
OnSucceededAsync, then waitsInterval. - After a poll that throws anything, an
OperationCanceledExceptionfrom a timeout included: logs a warning{Name} poll failed ({Failures} in a row); next poll in {Delay}with the exception, callsOnFailedAsync(exception), then waitsBackoff(Interval, failures, MaxBackoff): the interval doubled per consecutive failure after the first, at mostMaxBackoff(default 5 minutes). One success resets the count. - A hook that throws is logged as an error,
{Name} {Hook} failed, and the loop goes on; a failedOnSucceededAsyncdoes not count as a failed poll. - When the host stops, the poll or the wait in progress is cancelled through its token and
ExecuteAsyncreturns without an error. An exception a poll throws once the host is stopping is not caught.
| Member | Required | Use |
|---|---|---|
Name |
yes | Names the loop in every log line. |
Interval |
yes | The wait after a success. A property, so it may come from options that change at run time. |
PollAsync(ct) |
yes | One poll. Throw to report a failure. |
MaxBackoff |
no, 5 minutes | Cap on the wait after failures. Return Interval to retry at the normal pace. With an Interval above the cap, the first retry comes sooner than the next regular poll would. |
OnSucceededAsync(ct), OnFailedAsync(exception, ct) |
no | Record health, update a status page. |
Time, Logger |
The constructor's clock and logger, for subclasses (a primary-constructor parameter passed to the base and also captured is warning CS9107). |
Testing: every wait goes through the TimeProvider passed in, so FakeTimeProvider (Microsoft.Extensions.TimeProvider.Testing) drives the loop without real time passing.
Native AOT: works. samples/Switchknife.AotSmoke runs a poller in a real host under Native AOT: the first poll throws, the host keeps running, the second poll succeeds, the host stops cleanly.
Limits:
- One poll at a time; the next wait starts after the poll and its hook end, so the period is the poll's duration plus the wait, not a fixed rate.
- No scope per poll: resolve scoped services yourself with
IServiceScopeFactoryinsidePollAsync. IntervalandMaxBackoffare not validated, and an exception from their getters is not caught: both are configuration errors.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.13)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.13)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0 | 39 | 10/14/2026 |