Skip to content

Commit

Permalink
Make NativeAOT work on .NET 8.0 (#2759)
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan authored Feb 25, 2025
1 parent d4acdbd commit 4768f95
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/ReverseProxy/Configuration/IYarpOutputCachePolicyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#if NET7_0_OR_GREATER
using System;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.Extensions.Options;
Expand All @@ -24,6 +25,10 @@ internal interface IYarpOutputCachePolicyProvider
internal sealed class YarpOutputCachePolicyProvider : IYarpOutputCachePolicyProvider
{
#if NET7_0_OR_GREATER
// Workaround for https://github.com/dotnet/yarp/issues/2598 to make YARP work with NativeAOT on .NET 8. This is not needed on .NET 9+.
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
private static readonly Type s_OutputCacheOptionsType = typeof(OutputCacheOptions);

private readonly OutputCacheOptions _outputCacheOptions;

private readonly IDictionary _policyMap;
Expand All @@ -32,14 +37,12 @@ public YarpOutputCachePolicyProvider(IOptions<OutputCacheOptions> outputCacheOpt
{
_outputCacheOptions = outputCacheOptions?.Value ?? throw new ArgumentNullException(nameof(outputCacheOptions));

var type = typeof(OutputCacheOptions);
var flags = BindingFlags.Instance | BindingFlags.NonPublic;
var proprety = type.GetProperty("NamedPolicies", flags);
if (proprety == null || !typeof(IDictionary).IsAssignableFrom(proprety.PropertyType))
var property = s_OutputCacheOptionsType.GetProperty("NamedPolicies", BindingFlags.Instance | BindingFlags.NonPublic);
if (property == null || !typeof(IDictionary).IsAssignableFrom(property.PropertyType))
{
throw new NotSupportedException("This version of YARP is incompatible with the current version of ASP.NET Core.");
}
_policyMap = (proprety.GetValue(_outputCacheOptions, null) as IDictionary) ?? new Dictionary<string, object>();
_policyMap = (property.GetValue(_outputCacheOptions, null) as IDictionary) ?? new Dictionary<string, object>();
}

public ValueTask<object?> GetPolicyAsync(string policyName)
Expand Down

0 comments on commit 4768f95

Please # to comment.