Make DependencyInjection more linker trimmable #38678
Labels
area-Extensions-DependencyInjection
linkable-framework
Issues associated with delivering a linker friendly framework
Milestone
In Microsoft.Extensions.DependencyInjection, there are a few different "engines" that decide how to instantiate services, and how to inject those services into objects using those services.
Options are:
The way it decides which to pick is based on an enum:
runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProvider.cs
Lines 31 to 63 in 8b50c51
However, this enum is internal, and is only used in unit testing. An actual consumer will only ever get the
ServiceProviderMode.Default
option.runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProviderMode.cs
Lines 7 to 13 in 8b50c51
runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProviderOptions.cs
Line 28 in e3ffd34
The issue is, the way the above code is written, none of unused options are being trimmed, even though they are never used. This means that all the code, and its dependencies (ILEmit, Linq.Expressions) are being preserved, even though they are not being used.
We should change DependencyInjection so that these unused implementations can be trimmed by the linker.
Also we should change:
runtime/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProvider.cs
Line 37 in e3ffd34
to be
Because on WASM the linker will see that property is hard-coded to
false
and the unused branch will be trimmed. See:runtime/src/mono/netcore/System.Private.CoreLib/src/ILLink/ILLink.Substitutions.wasm.xml
Lines 9 to 11 in 2125ac5
The text was updated successfully, but these errors were encountered: