Closed darkthread closed 9 months ago
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection See info in area-owners.md if you want to be subscribed.
Author: | darkthread |
---|---|
Assignees: | - |
Labels: | `untriaged`, `area-Extensions-DependencyInjection` |
Milestone: | - |
I believe this is a documented breaking change here https://learn.microsoft.com/dotnet/core/compatibility/extensions/8.0/activatorutilities-createinstance-behavior
And is further discussed in this issue https://github.com/dotnet/runtime/issues/94736
I believe this is a documented breaking change here https://learn.microsoft.com/dotnet/core/compatibility/extensions/8.0/activatorutilities-createinstance-behavior
And is further discussed in this issue #94736
Thanks for the information. It seems that the breaking change has really rendered the ActivatorUtilitiesConstructorAttribute useless. It is suggested to include relevant explanations about ActivatorUtilitiesConstructorAttribute changes in the release notes and document to make the information more complete.
Description
ActivatorUtilitiesConstructorAttribute can help
ActivatorUtilities.CreateInstance()
to choose the preferred constructor of mutilple-contructor type. It works fine in .NET 6/7, and failed in .NET 8.Reproduction Steps
dotnet new web -o net8-issue
to create a empt web project.builder.Services.AddSingleton();
builder.Services.AddSingleton();
builder.Services.AddSingleton(sp => ActivatorUtilities.CreateInstance(sp));
var app = builder.Build(); Console.WriteLine($".NET {Environment.Version}"); try { var scope = app.Services.CreateScope(); var svc = scope.ServiceProvider.GetRequiredService();
Console.WriteLine("MyService created successfully");
}
catch (Exception ex)
{
Console.WriteLine("MyService failed to create");
Console.WriteLine(ex.Message);
}
public class Foo { } public class Bar { } public class MyService { [ActivatorUtilitiesConstructor] public MyService(Foo foo, IWebHostEnvironment env) { } public MyService(Bar bar, IWebHostEnvironment env) { } }
.NET 8.0.0 MyService created successfully
.NET 8.0.0 MyService failed to create Multiple constructors for type 'MyService' were found with length 2.
.NET 7.0.14 MyService created successfully