Skip to content

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

@jlaanstra

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>

Metadata

Metadata

Assignees

Labels

area-Tools-ILLink.NET linker development as well as trimming analyzersin-prThere is an active PR which will close this issue when it is merged

Type

No type
No fields configured for issues without a type.

Projects

Status

No status

Relationships

None yet

Development

No branches or pull requests

Issue actions