Skip to content

Annotate MemoryCache trimming requirements - #132662

Open
iamhaseebn wants to merge 1 commit into
dotnet:mainfrom
iamhaseebn:fix-runtime-caching-aot
Open

Annotate MemoryCache trimming requirements#132662
iamhaseebn wants to merge 1 commit into
dotnet:mainfrom
iamhaseebn:fix-runtime-caching-aot

Conversation

@iamhaseebn

Copy link
Copy Markdown

MemoryCache reads its optional configuration through ConfigurationManager, which is not compatible with trimming. This can currently build without warnings and then fail at runtime in a NativeAOT application.

Annotate both public constructors and the MemoryCache.Default accessor with RequiresUnreferencedCode so unsupported usage produces IL2026 during build. The three-argument constructor notes that the warning can be suppressed when ignoreConfigSection is true. The runtime behavior remains unchanged.

The annotations are mirrored in the reference assembly. Existing tests suppress the warning because their linker descriptor preserves the configuration constructor required by those scenarios.

Validated with:

  • dotnet build ./src/System.Runtime.Caching.csproj --no-restore
  • dotnet build /t:test ./tests/System.Runtime.Caching.Tests.csproj -f net11.0 - 46 tests, 0 failures, 1 existing platform skip
  • dotnet build ./tests/System.Runtime.Caching.Tests.csproj -f net481 --no-restore
  • dotnet build ./tests/System.Runtime.Caching.Tests.csproj -f net11.0-windows --no-restore
  • A clean NativeAOT consumer build, which now reports the intended IL2026 warning

Fixes #102341

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Aug 22, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime.Caching community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants