dotnet-hosted-service-testing

Guides writing and reviewing .NET BackgroundService tests with FakeTimeProvider and TimeProvider.

2|Updated Jul 18, 2026
One-click install
npx skills add https://github.com/Arasz/ai-badger --skill dotnet-hosted-service-testing-arasz
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-hosted-service-testing
Source: https://github.com/Arasz/ai-badger/tree/main/features/dotnet/skills/dotnet-workload/references/dotnet-hosted-service-testing
Command: npx skills add https://github.com/Arasz/ai-badger --skill dotnet-hosted-service-testing-arasz

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires Microsoft.Extensions.TimeProvider.Testing, Microsoft.Extensions.Hosting.Abstractions, and includes scripts (resource) and references (resource) components.

What problem does it solve? Testing .NET BackgroundService loops with FakeTimeProvider is full of silent traps: the first Advance is reliably lost, timer callbacks fire inline while continuations land on the threadpool, and tests can pass vacuously by counting side effects instead of loop invocations. This Skill encodes empirically verified semantics so hosted-service tests honestly cover the loop, interval, dedup, and DI-registration paths. ## Core Features & Use Cases - Verified FakeTimeProvider semantics: Documents lost-first-Advance behavior, inline-vs-threadpool callback threading, and PeriodicTimer double-fire behavior measured on .NET 10. - Test-honesty checklist: Detects vacuous gates, negative-only assertions, untested SQL surfaces, and missing DI registration smoke tests (the AddHostedService triple). - Empirical probe script: Runs a scratch console app that prints thread IDs and counters around StartAsync/Advance to settle timing questions in about two minutes. - Use Case: When reviewing a PR that adds an idle-watchdog BackgroundService with a 2-second timeout, use this Skill to catch that a fixed 60-second tick makes the shutdown gate unsatisfiable and derive the correct tick from the timeout. ## Quick Start Review my BackgroundService tests that use FakeTimeProvider and tell me whether the Advance assertions and DI registration tests are honest.

Frequently Asked Questions about dotnet-hosted-service-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I test a .NET BackgroundService with FakeTimeProvider?

Start the service, then account for the fact that StartAsync dispatches ExecuteAsync to a threadpool thread, so the first Advance is reliably lost. After each Advance, wait in real time for the Task.Delay continuation to land before asserting, and count loop invocations rather than side effects.

Why is my first FakeTimeProvider Advance ignored in BackgroundService tests?

StartAsync returns before ExecuteAsync registers its Task.Delay timer on a threadpool thread, so an Advance issued immediately afterward fires before any timer exists and is silently lost. Sync on the service's own startup log event instead of advancing immediately.

Does AddHostedService register the concrete type in .NET DI?

No, AddHostedService<T> only registers IHostedService to T, so resolving T directly throws InvalidOperationException. Register the concrete type, the interface, and the hosted service as three singleton registrations resolving the same instance, and pin it with a reference-equality smoke test.

How do I test a watchdog service with a short timeout and PeriodicTimer?

Derive the tick from the timeout, such as tick equals timeout divided by four, because a fixed 60-second tick makes short-timeout shutdown gates unsatisfiable. Pin the cadence with split Advances that stay below the deadline until the cadence is established, then cross it.

Why does my fake-clock test pass alone but fail under the full suite?

Parallel suite load can starve the real-time settle delays that fake-clock tests need after each Advance. Rebuild after test edits, since --no-build against a stale assembly reports phantom failures, then re-run under full-suite load before declaring the test flaky.