From 7ef816467bb5bd0e82f08898a800e546ca0f86b9 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Wed, 24 May 2023 09:29:13 -0500 Subject: [PATCH] Enable AOT Compatibility (#2144) Add IsAotCompatible=true to Yarp.ReverseProxy, which enables trimming and AOT analyzers. Address the current warnings. --- src/ReverseProxy/Forwarder/ForwarderTelemetry.cs | 5 +++++ .../Management/ReverseProxyServiceCollectionExtensions.cs | 7 ++++--- .../WebSocketsTelemetry/WebSocketsTelemetry.cs | 3 +++ src/ReverseProxy/Yarp.ReverseProxy.csproj | 1 + 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/ReverseProxy/Forwarder/ForwarderTelemetry.cs b/src/ReverseProxy/Forwarder/ForwarderTelemetry.cs index d2f673b2f..5a93738c1 100644 --- a/src/ReverseProxy/Forwarder/ForwarderTelemetry.cs +++ b/src/ReverseProxy/Forwarder/ForwarderTelemetry.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Tracing; using System.Threading; @@ -122,6 +123,8 @@ protected override void OnEventCommand(EventCommandEventArgs command) } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "Parameters to this method are primitive and are trimmer safe.")] [NonEvent] private unsafe void WriteEvent(int eventId, bool arg1, long arg2, long arg3, long arg4, long arg5) { @@ -157,6 +160,8 @@ private unsafe void WriteEvent(int eventId, bool arg1, long arg2, long arg3, lon WriteEventCore(eventId, NumEventDatas, descrs); } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "Parameters to this method are primitive and are trimmer safe.")] [NonEvent] private unsafe void WriteEvent(int eventId, bool arg1, long arg2, long arg3, long arg4, long arg5, long arg6) { diff --git a/src/ReverseProxy/Management/ReverseProxyServiceCollectionExtensions.cs b/src/ReverseProxy/Management/ReverseProxyServiceCollectionExtensions.cs index 9cdb0bcf7..d08fe6258 100644 --- a/src/ReverseProxy/Management/ReverseProxyServiceCollectionExtensions.cs +++ b/src/ReverseProxy/Management/ReverseProxyServiceCollectionExtensions.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Net.Http; using Microsoft.Extensions.Configuration; @@ -87,7 +88,7 @@ public static IReverseProxyBuilder LoadFromConfig(this IReverseProxyBuilder buil /// Registers a singleton IProxyConfigFilter service. Multiple filters are allowed and they will be run in registration order. /// /// A class that implements IProxyConfigFilter. - public static IReverseProxyBuilder AddConfigFilter(this IReverseProxyBuilder builder) where TService : class, IProxyConfigFilter + public static IReverseProxyBuilder AddConfigFilter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TService>(this IReverseProxyBuilder builder) where TService : class, IProxyConfigFilter { if (builder is null) { @@ -118,7 +119,7 @@ public static IReverseProxyBuilder AddTransforms(this IReverseProxyBuilder build /// Provides a implementation that will be run for each route to conditionally add transforms. /// can be called multiple times to provide multiple distinct types. /// - public static IReverseProxyBuilder AddTransforms(this IReverseProxyBuilder builder) where T : class, ITransformProvider + public static IReverseProxyBuilder AddTransforms<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(this IReverseProxyBuilder builder) where T : class, ITransformProvider { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); return builder; @@ -129,7 +130,7 @@ public static IReverseProxyBuilder AddTransforms(this IReverseProxyBuilder bu /// the associated transform actions. can be called multiple /// times to provide multiple distinct types. /// - public static IReverseProxyBuilder AddTransformFactory(this IReverseProxyBuilder builder) where T : class, ITransformFactory + public static IReverseProxyBuilder AddTransformFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(this IReverseProxyBuilder builder) where T : class, ITransformFactory { builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton()); return builder; diff --git a/src/ReverseProxy/WebSocketsTelemetry/WebSocketsTelemetry.cs b/src/ReverseProxy/WebSocketsTelemetry/WebSocketsTelemetry.cs index 103180f34..9457c9c81 100644 --- a/src/ReverseProxy/WebSocketsTelemetry/WebSocketsTelemetry.cs +++ b/src/ReverseProxy/WebSocketsTelemetry/WebSocketsTelemetry.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. +using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Tracing; namespace Yarp.ReverseProxy.WebSocketsTelemetry; @@ -10,6 +11,8 @@ internal sealed class WebSocketsTelemetry : EventSource { public static readonly WebSocketsTelemetry Log = new(); + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", + Justification = "Parameters to this method are primitive and are trimmer safe.")] [Event(1, Level = EventLevel.Informational)] public void WebSocketClosed(long establishedTime, WebSocketCloseReason closeReason, long messagesRead, long messagesWritten) { diff --git a/src/ReverseProxy/Yarp.ReverseProxy.csproj b/src/ReverseProxy/Yarp.ReverseProxy.csproj index 8755b7eb8..d71f3bf33 100644 --- a/src/ReverseProxy/Yarp.ReverseProxy.csproj +++ b/src/ReverseProxy/Yarp.ReverseProxy.csproj @@ -7,6 +7,7 @@ Yarp.ReverseProxy true enable + true README.md