[Breaking change]: ActivatorUtilities.CreateInstance(...) no longer allows null
for the provider
parameter
#34158
Closed
1 of 3 tasks
Labels
breaking-change
Indicates a .NET Core breaking change
🏁 Release: .NET 8
Work items for the .NET 8 release
Description
For the class
Microsoft.Extensions.DependencyInjection.ActivatorUtilities
, the two CreateInstance() methods and the ObjectFactory delegate returned from CreateFactory() will now throwArgumentNullException
when theprovider
parameter isnull
.Version
.NET 8 Preview 1 (CreateInstance)
.NET 8 Preview 4 (ObjectFactory)
Previous behavior
A
null
value was allowed for theprovider
parameter and did create the specified type correctly for some cases.New behavior
When
provider
isnull
,ArgumentNullException
is thrown.Type of breaking change
Reason for change
Various constructor-matching issues were fixed along with parameter validation to match the intended purpose of
CreateInstance()
. TheCreateInstance()
methods have a non-nullableprovider
parameter, so it was generally expected that anull
provider was not allowed.See also the breaking change #31785 which covers the constructor-matching changes.
Recommended action
Pass a non-null
IServiceProvider
for theprovider
argument. If the provider also implementsIServiceProviderIsService
then constructor arguments may be obtained through that.Alternatively, if your scenario doesn't require dependency injection, since
IServiceProvider
isnull
, use Activator.CreateInstance instead.Feature area
Core .NET libraries, Extensions
Affected APIs
On the class
Microsoft.Extensions.DependencyInjection.ActivatorUtilities
:The CreateInstance changed in PR dotnet/runtime#75846
The ObjectFactory changes made in dotnet/runtime#82739 and dotnet/runtime#85065.
The text was updated successfully, but these errors were encountered: