Skip to content

Commit

Permalink
Enable AOT Compatibility (#2144)
Browse files Browse the repository at this point in the history
Add IsAotCompatible=true to Yarp.ReverseProxy, which enables trimming and AOT analyzers.

Address the current warnings.
  • Loading branch information
eerhardt authored May 24, 2023
1 parent ad6ee6a commit 7ef8164
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/ReverseProxy/Forwarder/ForwarderTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Threading;

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
/// </summary>
/// <typeparam name="TService">A class that implements IProxyConfigFilter.</typeparam>
public static IReverseProxyBuilder AddConfigFilter<TService>(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)
{
Expand Down Expand Up @@ -118,7 +119,7 @@ public static IReverseProxyBuilder AddTransforms(this IReverseProxyBuilder build
/// Provides a <see cref="ITransformProvider"/> implementation that will be run for each route to conditionally add transforms.
/// <see cref="AddTransforms{T}(IReverseProxyBuilder)"/> can be called multiple times to provide multiple distinct types.
/// </summary>
public static IReverseProxyBuilder AddTransforms<T>(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<ITransformProvider, T>());
return builder;
Expand All @@ -129,7 +130,7 @@ public static IReverseProxyBuilder AddTransforms<T>(this IReverseProxyBuilder bu
/// the associated transform actions. <see cref="AddTransformFactory{T}(IReverseProxyBuilder)"/> can be called multiple
/// times to provide multiple distinct types.
/// </summary>
public static IReverseProxyBuilder AddTransformFactory<T>(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<ITransformFactory, T>());
return builder;
Expand Down
3 changes: 3 additions & 0 deletions src/ReverseProxy/WebSocketsTelemetry/WebSocketsTelemetry.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions src/ReverseProxy/Yarp.ReverseProxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<RootNamespace>Yarp.ReverseProxy</RootNamespace>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<IsAotCompatible>true</IsAotCompatible>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

Expand Down

0 comments on commit 7ef8164

Please sign in to comment.