Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<linker>
<assembly fullname="Microsoft.Extensions.Caching.Abstractions">
<!-- Accessed via [UnsafeAccessor] from Microsoft.Extensions.Caching.Hybrid, which the trimmer cannot follow. -->
<type fullname="Microsoft.Extensions.Caching.Hybrid.HybridCacheEntryOptions">
<method name="ToDistributedCacheEntryOptions" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Hybrid;

class Program
{
// Mirrors the access pattern used by Microsoft.Extensions.Caching.Hybrid's DefaultHybridCache.
// HybridCacheEntryOptions.ToDistributedCacheEntryOptions is internal and is only reached via
// [UnsafeAccessor] from another assembly, which the trimmer cannot follow. It must be preserved
// by the ILLink.Descriptors.xml in Microsoft.Extensions.Caching.Abstractions.
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "ToDistributedCacheEntryOptions")]
extern static DistributedCacheEntryOptions? ToDistributedCacheEntryOptions(HybridCacheEntryOptions options);

static int Main()
{
TimeSpan expiration = TimeSpan.FromMinutes(5);
HybridCacheEntryOptions options = new() { Expiration = expiration };

// Throws MissingMethodException if the target method was trimmed away.
DistributedCacheEntryOptions? result = ToDistributedCacheEntryOptions(options);

if (result is null || result.AbsoluteExpirationRelativeToNow != expiration)
{
return -1;
}

return 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project DefaultTargets="Build">
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.props))" />

<ItemGroup>
<TestConsoleAppSourceFiles Include="HybridCacheEntryOptionsAccessorTest.cs" />
</ItemGroup>

<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets))" />
</Project>
Loading