Skip to content

.NET 9 trimmed application fails with System.TypeLoadException (Method trimmed but interface still listed on type). #109850

Closed

Description

@jlaanstra

Description

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.

Regression?

Yes, from .NET 8

Known Workarounds

No response

Configuration

.NET 9 on Windows 11, x64, running as AnyCPU.

Other information

Project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
      <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0" />
  </ItemGroup>

</Project>

Activity

  1. added
    untriaged
    on Nov 14, 2024
  2. dotnet-policy-service commented on Nov 14, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection
    See info in area-owners.md if you want to be subscribed.

  3. jlaanstra commented on Nov 14, 2024

    @jlaanstra
    ContributorAuthor

    Also @dotnet/illink

  4. added
    area-Tools-ILLink
    and removed on Nov 15, 2024
  5. dotnet-policy-service commented on Nov 15, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas
    See info in area-owners.md if you want to be subscribed.

  6. added this to the 9.0.x milestone on Nov 17, 2024
  7. removed
    untriaged
    on Nov 17, 2024
  8. sbomer commented on Nov 18, 2024

    @sbomer
    Member

    @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!

  9. jlaanstra commented on Nov 18, 2024

    @jlaanstra
    ContributorAuthor

    @sbomer Yes to using PublishTrimmed.

    C:\Program Files\Microsoft Visual Studio\2022\Enterprise>dotnet --info
    .NET SDK:
     Version:           9.0.100
     Commit:            59db016f11
     Workload version:  9.0.100-manifests.c6f19616
     MSBuild version:   17.12.7+5b8665660
    
    Runtime Environment:
     OS Name:     Windows
     OS Version:  10.0.26120
     OS Platform: Windows
     RID:         win-x64
     Base Path:   C:\Program Files\dotnet\sdk\9.0.100\
    
    .NET workloads installed:
    There are no installed workloads to display.
    Configured to use loose manifests when installing new manifests.
    
    Host:
      Version:      9.0.0
      Architecture: x64
      Commit:       9d5a6a9aa4
    
    .NET SDKs installed:
      6.0.427 [C:\Program Files\dotnet\sdk]
      7.0.120 [C:\Program Files\dotnet\sdk]
      8.0.110 [C:\Program Files\dotnet\sdk]
      8.0.403 [C:\Program Files\dotnet\sdk]
      9.0.100 [C:\Program Files\dotnet\sdk]
    
    .NET runtimes installed:
      Microsoft.AspNetCore.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.AspNetCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.NETCore.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.NETCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.NETCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.NETCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
      Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
      Microsoft.WindowsDesktop.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
      Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
      Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
      Microsoft.WindowsDesktop.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
      Microsoft.WindowsDesktop.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
      Microsoft.WindowsDesktop.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
    
    Other architectures found:
      x86   [C:\Program Files (x86)\dotnet]
        registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
    
    Environment variables:
      Not set
    
    global.json file:
      Not found
    
    Learn more:
      https://aka.ms/dotnet/info
    
    Download .NET:
      https://aka.ms/dotnet/download
    

    Attached is a repro project. Select the Debug,AnyCPU config, then publish from VS, then run the published exe.

    ConsoleApp8.zip

  10. added
    in-pr
    on Nov 23, 2024
  11. sbomer commented on Nov 23, 2024

    @sbomer
    Member

    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:

    ProcessInterfaceMethods ();
    ProcessMarkedTypesWithInterfaces ();

    #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.

  12. ghost1372 commented on Feb 22, 2025

    @ghost1372

    I have this problem too. When will this be fixed? I can't publish my app as AOT/Trimmed.

  13. aries-zhang commented on Apr 7, 2025

    @aries-zhang

    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?

  14. sbomer commented on Apr 7, 2025

    @sbomer
    Member

    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!

  15. aries-zhang commented on Apr 8, 2025

    @aries-zhang

    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

  16. sbomer commented on Apr 10, 2025

    @sbomer
    Member

    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.

  17. locked and limited conversation to collaborators on May 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Tools-ILLinkin-pr

Type

No type

Projects

  • Status
    No status

Relationships

None yet

Development

No branches or pull requests

Issue actions