Skip to content

[FEATURE] 🚀 Add explicit AOT compatibility #449

@jodydonetti

Description

@jodydonetti

Discussed in #439

Originally posted by **digital88** March 31, 2025

Problem

FusionCache has been AOT compatible for a long time, which is already good.
I just need to make that more "official" by declaring it in the csproj, enabling analyzers, create a test console app and, in general, do everything that's needed.

Solution

As explained here here's what I already did.

I created an AOTTester console app with this code:

using ZiggyCreatures.Caching.Fusion;

namespace AOTTester;

internal class Program
{
	static async Task Main(string[] args)
	{
		var cache = new FusionCache(new FusionCacheOptions());

		var value = await cache.GetOrSetAsync<int>("foo", async (ctx, ct) => 42);

		Console.WriteLine($"VALUE: {value}");
	}
}

The csproj looks like this:

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

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

		<PublishAot>true</PublishAot>
		<PublishSingleFile>true</PublishSingleFile>
		<SelfContained>true</SelfContained>
		<PublishTrimmed>true</PublishTrimmed>
	</PropertyGroup>

	<ItemGroup>
		<ProjectReference Include="..\..\src\ZiggyCreatures.FusionCache\ZiggyCreatures.FusionCache.csproj" />
		<TrimmerRootAssembly Include="ZiggyCreatures.FusionCache" />
	</ItemGroup>

</Project>

Some elements may not be 100% needed since they are implicit when you specify others, but still.

Then, without even touching FusionCache csproj file, I run this:

dotnet publish -c Release --ucr --self-contained

which told me this:

Restore complete (0.4s)
  ZiggyCreatures.FusionCache net9.0 succeeded (1.7s) → C:\[...]\ZiggyCreatures.FusionCache\bin\Release\net9.0\win-x64\ZiggyCreatures.FusionCache.dll
  AOTTester succeeded (4.6s) → bin\Release\net9.0\win-x64\publish\

Build succeeded in 7.0s

No errors, no warnings, resulting in this:

image

Then I tried adding this to the FusionCache csproj file:

<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsAotCompatible>

Then run again the publish: no difference, exactly the same results.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions