-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActivatorUtilities.CreateInstance Throwing InvalidOperationException in .NET 8 #94736
Comments
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsDescriptionAfter upgrading to .NET 8, ActivatorUtilties.CreateInstance is throwing InvalidOperationException when the following conditions are met
Reproduction Steps internal class Program
{
static void Main(string[] args)
{
_ = ActivatorUtilities.CreateInstance(new EmptyServiceProvider(), typeof(TestClass));
}
class TestClass
{
public TestClass()
{
}
/* Comment out this constructor and the call will succeed */
public TestClass(int value)
{
}
}
class EmptyServiceProvider : IServiceProvider
{
public object? GetService(Type serviceType)
{
return null;
}
}
} Expected behaviorCall the default constructor. Actual behaviorThrowing InvalidOperationException, with message "Multiple constructors accepting all given argument types have been found in type ..." Regression?Yes this worked in .NET 7 and prior. Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
I believe you're supposed to be using Although it does seem that the behavior of this method was changed for .NET 8 in PR #75846 |
I'll also add that this occurs when the container does not support using IServiceProviderIsService. The exception is being thrown from TryFindMatchingConstructor. |
CC @steveharter |
Added
Tagging @dotnet/compat for awareness of the breaking change. |
Correct; I verified this issue was due to the check\usage of Assuming we make no changes, the workarounds are:
The original PR was marked with the |
Closing as by design based on the breaking change notification linked above. |
Description
After upgrading to .NET 8, ActivatorUtilties.CreateInstance is throwing InvalidOperationException when the following conditions are met
Reproduction Steps
Expected behavior
Call the default constructor.
Actual behavior
Throwing InvalidOperationException, with message "Multiple constructors accepting all given argument types have been found in type ..."
Regression?
Yes this worked in .NET 7 and prior.
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: