Stop Pretending TimeProvider Doesn't Exist — Daily DevOps & .NET

This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).

DateTime.UtcNow is a hidden dependency that makes time-sensitive code untestable and causes hard-to-reproduce production bugs. .NET 8 introduced TimeProvider, an official abstraction in the System namespace that replaces ad-hoc IClock patterns. By injecting TimeProvider and using FakeTimeProvider from Microsoft.Extensions.TimeProvider.Testing, developers can freeze and advance time deterministically in tests — no Thread.Sleep, no flaky timing windows. The post covers rewriting token validators, testing retry backoff logic, migrating away from custom IClock interfaces, and a practical step-by-step adoption path for existing codebases. ASP.NET Core's own ISystemClock was deprecated in .NET 8 in favor of TimeProvider, signaling the platform's clear direction.

8m read timeFrom daily-devops.net
Post cover image
Table of contents
The Problem With DateTime.UtcNowWhat TimeProvider IsRewriting the ExampleTesting With FakeTimeProviderThe Timer Problem Nobody MentionsWhat Already Uses TimeProviderThe IClock Pattern Is DeadWhen You Cannot Inject TimeProviderThe One RuleStart Monday, Not Next Quarter
1 Comment

Sort: