-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Description
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.