extensions
extensions copied to clipboard
Microsoft.Extensions.DependencyInjection.AutoActivation is not working.
Description
Microsoft.Extensions.DependencyInjection.AutoActivation is not working.
Reproduction Steps
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddActivatedSingleton<MyService>();
Console.WriteLine("Build Before");
ServiceProvider provider = services.BuildServiceProvider();
Console.WriteLine("Build After");
provider.GetRequiredService<MyService>();
Console.WriteLine("End");
class MyService
{
public MyService() => Console.WriteLine("MyService Created");
}
Expected behavior
The expected output should be, in order:
"Build Before"
"MyService Created"
"Build After"
"End"
Actual behavior
The actual output is:
"Build Before"
"Build After"
"MyService Created"
"End"
Regression?
No response
Known Workarounds
No response
Configuration
Microsoft.Extensions.DependencyInjection: 10.0.7
Microsoft.Extensions.DependencyInjection.AutoActivation: 10.5.0
.NET SDK:
Version: 10.0.203
Commit: c23858a6d8
Workload version: 10.0.200-manifests.f8ca1cb9
MSBuild version: 18.3.3+c23858a6d
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.203\
Other information
No response
Based on my investigation, an IHost object is required for this to work, so the .NET Generic Host model needs to be used. Without it, AddActivatedSingleton doesn't actually auto-activate.
Therefore, the examples in this article are invalid.