Skip to content
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

[Breaking change]: ActivatorUtilities.CreateInstance(...) no longer allows null for the provider parameter #34158

Closed
1 of 3 tasks
steveharter opened this issue Feb 17, 2023 · 1 comment · Fixed by #33714
Closed
1 of 3 tasks
Assignees
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 8 Work items for the .NET 8 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3

Comments

@steveharter
Copy link
Member

steveharter commented Feb 17, 2023

Description

For the class Microsoft.Extensions.DependencyInjection.ActivatorUtilities, the two CreateInstance() methods and the ObjectFactory delegate returned from CreateFactory() will now throw ArgumentNullException when the provider parameter is null.

Version

.NET 8 Preview 1 (CreateInstance)
.NET 8 Preview 4 (ObjectFactory)

Previous behavior

A null value was allowed for the provider parameter and did create the specified type correctly for some cases.

New behavior

When provider is null, ArgumentNullException is thrown.

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
  • Behavioral change: Existing binaries may behave differently at run time.

Reason for change

Various constructor-matching issues were fixed along with parameter validation to match the intended purpose of CreateInstance(). The CreateInstance() methods have a non-nullable provider parameter, so it was generally expected that a null provider was not allowed.

See also the breaking change #31785 which covers the constructor-matching changes.

Recommended action

Pass a non-null IServiceProvider for the provider argument. If the provider also implements IServiceProviderIsService then constructor arguments may be obtained through that.

Alternatively, if your scenario doesn't require dependency injection, since IServiceProvider is null, use Activator.CreateInstance instead.

Feature area

Core .NET libraries, Extensions

Affected APIs

On the class Microsoft.Extensions.DependencyInjection.ActivatorUtilities:

public static object CreateInstance (IServiceProvider provider, Type instanceType, params object[] parameters);
public static T CreateInstance<T> (IServiceProvider provider, params object[] parameters);
public static Microsoft.Extensions.DependencyInjection.ObjectFactory CreateFactory(IServiceProvider provider, params object[] parameters);

The CreateInstance changed in PR dotnet/runtime#75846

The ObjectFactory changes made in dotnet/runtime#82739 and dotnet/runtime#85065.

@steveharter steveharter added doc-idea Indicates issues that are suggestions for new topics [org][type][category] breaking-change Indicates a .NET Core breaking change Pri1 High priority, do before Pri2 and Pri3 labels Feb 17, 2023
@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Feb 17, 2023
@steveharter
Copy link
Member Author

cc @buyaa-n @eerhardt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change Indicates a .NET Core breaking change 🏁 Release: .NET 8 Work items for the .NET 8 release doc-idea Indicates issues that are suggestions for new topics [org][type][category] Pri1 High priority, do before Pri2 and Pri3
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants