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.
Using the following project:
When you
dotnet build
you don't get any warnings thatMemoryCache
isn't going to work in a trimmed or AOT'd application.However, when you
dotnet publish
the project, you do get a warning:Then when you run the resulting
.exe
it fails with:We should either make System.Runtime.Caching work with trimming/native AOT, or annotate it as incompatible with trimming.
This is related to https://github.com/dotnet/runtime/issues/75480, but I didn't see
System.Runtime.Caching
in the list on that issue.cc @agocke @LakshanF @MichalStrehovsky