Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jlaanstra opened this issue Nov 14, 2024 · 3 comments
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers untriaged New issue has not been triaged by the area owner

Comments

@jlaanstra
Copy link

jlaanstra commented Nov 14, 2024

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>
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 14, 2024
Copy link
Contributor

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

@jlaanstra
Copy link
Author

Also @dotnet/illink

@hoyosjs hoyosjs added area-Tools-ILLink .NET linker development as well as trimming analyzers and removed area-Extensions-DependencyInjection labels Nov 15, 2024
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Tools-ILLink .NET linker development as well as trimming analyzers untriaged New issue has not been triaged by the area owner
Projects
Status: No status
Development

No branches or pull requests

2 participants