dotnet / dotnet/aspnetcore

validating the service descriptor for PinnedBlockMemoryPoolFactory fails when a custom scoped TimeProvider is registered

Open
#67,957 0 comments 0 reactions 0 assignees View on GitHub
area-networking feature-kestrel
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues - found https://github.com/dotnet/aspnetcore/issues/64440 which seems to be the same problem

### Describe the bug

After updating my app from .NET 8 to .NET 10, I'm getting an exception on startup:

```
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Server.Kestrel.Core.Internal.PinnedBlockMemoryPoolFactory Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Server.Kestrel.Core.Internal.PinnedBlockMemoryPoolFactory': Cannot consume scoped service 'System.TimeProvider' from singleton 'Microsoft.AspNetCore.Server.Kestrel.Core.Internal.PinnedBlockMemoryPoolFactory'.)'
```

I have a custom `TimeProvider` registered as scoped (to tie it to the HTTP request start time). This used to work just fine in .NET 8, but now the singleton `PinnedBlockMemoryPoolFactory` injects `TimeProvider` causing the exception.

I was under the impression that custom `TimeProvider` registrations are allowed, at least there is nothing in the documentation I could find that would say otherwise. Am I wrong in using `TimeProvider` this way or is this an actual bug in .NET?

Since the internal `PinnedBlockMemoryPoolFactory` depends on public `TimeProvider` which might get overridden by app developers, shouldn't it resolve it via `IServiceScopeFactory` instead of injecting it directly?

### Expected Behavior

A .NET 10 app should be able to start without exceptions when a scoped `TimeProvider` is registered.

### Steps To Reproduce

Have this in `Program.cs`:
```
var builder = WebApplication.CreateBuilder(args);
services.AddScoped();
var app = builder.Build();
await app.RunAsync();

internal class CustomTimeProvider : TimeProvider { }
```

It throws an exception when running in development mode.

### Exceptions (if any)

`System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Server.Kestrel.Core.Internal.PinnedBlockMemoryPoolFactory Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Server.Kestrel.Core.Internal.PinnedBlockMemoryPoolFactory': Cannot consume scoped service 'System.TimeProvider' from singleton 'Microsoft.AspNetCore.Server.Kestrel.Core.Internal.PinnedBlockMemoryPoolFactory'.)'`

### .NET Version

10.0.302

### Anything else?

```
$ dotnet --info
.NET SDK:
Version: 10.0.302
Commit: 35b593bebf
Workload version: 10.0.300-manifests.714b12c0
MSBuild version: 18.6.11+35b593beb

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.302\

.NET workloads installed:
[wasm-tools]
Installation Source: VS 18.8.12009.203
Manifest Version: 10.0.110/10.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\10.0.100\microsoft.net.workload.mono.toolchain.current\10.0.110\WorkloadManifest.json
Install Type: Msi

Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.

Host:
Version: 10.0.10
Architecture: x64
Commit: f7d90799ce

.NET SDKs installed:
8.0.422 [C:\Program Files\dotnet\sdk]
9.0.205 [C:\Program Files\dotnet\sdk]
10.0.302 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 10.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 10.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 10.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download
```

Contributor guide

Open the contributing guide

Research direction

Start with the Program.cs reproduction and inspect the registration and construction path for PinnedBlockMemoryPoolFactory, especially its TimeProvider dependency. Confirm the behavior with a scoped CustomTimeProvider and add or update a regression test so the application starts successfully without the service-validation exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.