You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.NET 9 trimmed application fails with System.TypeLoadException (Method trimmed but interface still listed on type). #109850
After updating to .NET 9 a trimmed application fails in debug configuration with a System.TypeLoadException: Method 'GetKeyedService' in type 'Microsoft.Extensions.DependencyInjection.ServiceProvider' from assembly 'Microsoft.Extensions.DependencyInjection, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.
Based on inspecting the trimmed dll, the ServiceProvider has the IKeyedServiceProvider interface listed but none of the methods are present.
Reproduction Steps
Create a new console app and replace Main with:
internal class Program
{
static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddMemoryCache();
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
Console.WriteLine(serviceProvider.GetRequiredService<IMemoryCache>());
}
}
Publish the exe and run it to make sure the trimmed exe is ran.
Expected behavior
No TypeLoadException should be thrown. A trimmed application should work just like untrimmed since there are no trim warnings.
Actual behavior
Application fails in debug configuration with a System.TypeLoadException: Method 'GetKeyedService' in type 'Microsoft.Extensions.DependencyInjection.ServiceProvider' from assembly 'Microsoft.Extensions.DependencyInjection, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.
@jlaanstra I wasn't able to repro this using the code you shared. Did you publish with <PublishTrimmed>true</PublishTrimmed> in the csproj? Would you mind providing the exact repro steps (to publish and run) and dotnet version you are using? (dotnet --info output). Thanks!
In the repro, we mark ServiceProvider's impl of IKeyedServiceProvider, but then never reach ProcessInterfaceMethods (which is responsible for marking the methods required by any kept .impls).
Swapping the order of ProcessInterfaceMethods and ProcessMarkedTypesWithInterfaces here fixes the reported issue:
#110098 fixes the immediate issue, but I'm concerned there might be more subtle dependencies on the ordering of these two steps, so this will need more investigation. There's also still the open question of why this fails when publishing from VS, but not when publishing from the command-line.
Same critical dependency here which is blocking my app's native ATO too - @sbomer looks like your PR was closed, any idea if this is going to be fixed soon?
Looking at this again, I don't think we should block the fix on doing a more complete investigation of the ordering dependencies (this would ideally be addressed by onboarding to the native AOT analysis engine). I spent some time thinking about this and couldn't think of any problems that would be introduced by #110098, so I'll reopen it. Thanks!
There's also still the open question of why this fails when publishing from VS, but not when publishing from the command-line.
Is your command line building with the Release config? I tried with MSBuild in my project and it looks like it repros with /p:configuration="Debug" but not /p:configuration="Release", so I'm guessing if it's more relating to the build configuration not cmd vs VS. @sbomer
Thanks, I probably was building in a different configuration. In any case the original issue should be fixed in .NET 10, will see about backporting a fix to .NET 9.
Activity
dotnet-policy-service commentedon Nov 14, 2024
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection
See info in area-owners.md if you want to be subscribed.
jlaanstra commentedon Nov 14, 2024
Also @dotnet/illink
dotnet-policy-service commentedon Nov 15, 2024
Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas
See info in area-owners.md if you want to be subscribed.
sbomer commentedon Nov 18, 2024
@jlaanstra I wasn't able to repro this using the code you shared. Did you publish with
<PublishTrimmed>true</PublishTrimmed>in the csproj? Would you mind providing the exact repro steps (to publish and run) and dotnet version you are using? (dotnet --infooutput). Thanks!jlaanstra commentedon Nov 18, 2024
@sbomer Yes to using PublishTrimmed.
Attached is a repro project. Select the Debug,AnyCPU config, then publish from VS, then run the published exe.
ConsoleApp8.zip
sbomer commentedon Nov 23, 2024
In the repro, we mark ServiceProvider's impl of IKeyedServiceProvider, but then never reach
ProcessInterfaceMethods(which is responsible for marking the methods required by any kept .impls).Swapping the order of
ProcessInterfaceMethodsandProcessMarkedTypesWithInterfaceshere fixes the reported issue:runtime/src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Lines 418 to 419 in 35e23f7
#110098 fixes the immediate issue, but I'm concerned there might be more subtle dependencies on the ordering of these two steps, so this will need more investigation. There's also still the open question of why this fails when publishing from VS, but not when publishing from the command-line.
ghost1372 commentedon Feb 22, 2025
I have this problem too. When will this be fixed? I can't publish my app as AOT/Trimmed.
aries-zhang commentedon Apr 7, 2025
Same critical dependency here which is blocking my app's native ATO too - @sbomer looks like your PR was closed, any idea if this is going to be fixed soon?
sbomer commentedon Apr 7, 2025
Looking at this again, I don't think we should block the fix on doing a more complete investigation of the ordering dependencies (this would ideally be addressed by onboarding to the native AOT analysis engine). I spent some time thinking about this and couldn't think of any problems that would be introduced by #110098, so I'll reopen it. Thanks!
aries-zhang commentedon Apr 8, 2025
Is your command line building with the Release config? I tried with MSBuild in my project and it looks like it repros with
/p:configuration="Debug"but not/p:configuration="Release", so I'm guessing if it's more relating to the build configuration not cmd vs VS. @sbomersbomer commentedon Apr 10, 2025
Thanks, I probably was building in a different configuration. In any case the original issue should be fixed in .NET 10, will see about backporting a fix to .NET 9.