I know, I know, .NET 9 is released but I’m still helping one of my customers to move all workloads to .NET 8 first. Today we got a compiler error after upgrading our authentication code to .NET 8:
'ISystemClock' is obsolete: 'Use TimeProvider instead.'
1>C:\projects\IAMCore\IAM.Loket\IAMApiKeyAuthenticationHandler.cs(21,13,21,52): warning CS0618: 'AuthenticationHandler<IAMApiKeyAuthenticationOptions>.AuthenticationHandler(IOptionsMonitor<IAMApiKeyAuthenticationOptions>, ILoggerFactory, UrlEncoder, ISystemClock)' is obsolete: 'ISystemClock is obsolete, use TimeProvider on AuthenticationSchemeOptions instead.'
Sidenote: I know that this is a warning, but we got ‘Treat warnings as errors enabled’, a good practice I would recommend everyone to activate on their projects.
Here is the original code:
ISystemClock was an old abstraction to help during testing. It was never promoted as an official feature. With the release of .NET 8 we got the ‘official’ TimeProvider abstraction:
If you want to learn more about this TimeProvider, I would recommend to check my previous post about the TimeProvider and ITimer
The fix itself was easy, we updated our own AuthenticationHandler constructor and removed the ISystemClock parameter:
More information
.NET 8–It’s time to get rid of these flaky tests!
AuthenticationHandler<TOptions> Class (Microsoft.AspNetCore.Authentication) | Microsoft Learn