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

System.Runtime.Caching is not AOT compatible and isn't annotated #102341

Open
eerhardt opened this issue May 16, 2024 · 1 comment
Open

System.Runtime.Caching is not AOT compatible and isn't annotated #102341

eerhardt opened this issue May 16, 2024 · 1 comment
Labels
area-System.Runtime.Caching untriaged New issue has not been triaged by the area owner

Comments

@eerhardt
Copy link
Member

Using the following project:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
    <InvariantGlobalization>true</InvariantGlobalization>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Runtime.Caching" Version="8.0.0" />
  </ItemGroup>
</Project>
using System.Runtime.Caching;

MemoryCache memoryCache = new MemoryCache("MyCache");

memoryCache.Add("key", "value", DateTime.UtcNow.AddMinutes(1440.0));

Console.WriteLine(memoryCache.Get("key"));

When you dotnet build you don't get any warnings that MemoryCache isn't going to work in a trimmed or AOT'd application.

However, when you dotnet publish the project, you do get a warning:

.nuget\packages\system.configuration.configurationmanager\8.0.0\lib\net8.0\System.Configuration.ConfigurationManager.dll : warning IL2104: Assembly 'System.Configuration.ConfigurationManager' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [C:\Users\eerhardt\source\repos\ConsoleApp123\ConsoleApp123\ConsoleApp123.csproj]

Then when you run the resulting .exe it fails with:

Unhandled Exception: System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
 ---> System.MissingMethodException: No parameterless constructor defined for type 'System.Configuration.ClientConfigurationHost'.
   at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x119
   at System.Configuration.Internal.ConfigSystem.System.Configuration.Internal.IConfigSystem.Init(Type, Object[]) + 0x43
   at System.Configuration.ClientConfigurationSystem..ctor() + 0x47
   at System.Configuration.ConfigurationManager.EnsureConfigurationSystem() + 0x56
   --- End of inner exception stack trace ---
   at System.Configuration.ConfigurationManager.EnsureConfigurationSystem() + 0xea
   at System.Configuration.ConfigurationManager.PrepareConfigSystem() + 0x12
   at System.Configuration.ConfigurationManager.GetSection(String) + 0x18
   at System.Runtime.Caching.MemoryCacheStatistics.InitializeConfiguration(NameValueCollection) + 0x2c
   at System.Runtime.Caching.MemoryCacheStatistics..ctor(MemoryCache, NameValueCollection) + 0x4a
   at System.Runtime.Caching.MemoryCache.InitDisposableMembers(NameValueCollection) + 0xf1
   at Program.<Main>$(String[] args) + 0x26
   at ConsoleApp123!<BaseAddress>+0x3dc723

We should either make System.Runtime.Caching work with trimming/native AOT, or annotate it as incompatible with trimming.

This is related to #75480, but I didn't see System.Runtime.Caching in the list on that issue.

cc @agocke @LakshanF @MichalStrehovsky

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 16, 2024
@MichalStrehovsky
Copy link
Member

This looks like a trimming issue, not AOT issue.

The exception is in ConfigurationManager. Looks like we didn't annotate it as such, and closed the underlying issue as by design :(.

<!-- opt-out of trimming until it works https://github.com/dotnet/runtime/issues/49062 -->
<IsTrimmable>false</IsTrimmable>
<EnableAOTAnalyzer>false</EnableAOTAnalyzer>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Runtime.Caching untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants