Skip to content

Private reflection breaks .NET 6  #2490

@davidfowl

Description

@davidfowl

As part of .NET 6, we will want to move the ServiceCollection type to Microsoft.Extensions.DependencyInjection.Abstractions dotnet/runtime#52284. This broke OData since it uses private reflection to make this work 1.0 and 2.0 work without having to re-compile.

public virtual IServiceProvider BuildContainer()
{
/* "services.BuildServiceProvider()" returns IServiceProvider in Microsoft.Extensions.DependencyInjection 1.0 and ServiceProvider in Microsoft.Extensions.DependencyInjection 2.0
* (This is a breaking change)[https://github.com/aspnet/DependencyInjection/issues/550].
* To support both versions with the same code base in OData/WebAPI we decided to call that extension method using reflection.
* More info at https://github.com/OData/WebApi/pull/1082
*/
Assembly microsoftExtensionsDependencyInjectionAssembly = services.GetType().GetTypeInfo().Assembly;
TypeInfo serviceCollectionContainerBuilderExtensionsType = microsoftExtensionsDependencyInjectionAssembly.GetType(typeof(ServiceCollectionContainerBuilderExtensions).GetTypeInfo().FullName).GetTypeInfo();
MethodInfo buildServiceProviderMethod = serviceCollectionContainerBuilderExtensionsType.GetMethod("BuildServiceProvider", new[] { typeof(IServiceCollection) });
return (IServiceProvider)buildServiceProviderMethod.Invoke(null, new object[] { services });

It might be time to drop Microsoft.Extensions.DependencyInjection 1.0 support. That would clean this code up nicely.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions