Skip to content

Commit be641b6

Browse files
authoredMay 16, 2022
Re-enable trim analysis and fix warnings (grpc#1744)
1 parent 248c580 commit be641b6

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed
 

‎Directory.Build.targets

-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
<CodeAnalysisRuleset>$(MSBuildThisFileDirectory)Grpc.DotNet.ruleset</CodeAnalysisRuleset>
2020

2121
<IsTrimmable>true</IsTrimmable>
22-
<!-- TODO(JamesNK) Trim anaylzers broken in .NET 7. Impacts benchmark environments that build with .NET 7 SDK. Issue: https://github.com/grpc/grpc-dotnet/issues/1667
23-
Reenable when https://github.com/dotnet/linker/issues/2718 is fixed. -->
24-
<EnableTrimAnalyzer>false</EnableTrimAnalyzer>
2522
</PropertyGroup>
2623

2724
<!-- IsGrpcPublishedPackage is set in csproj so related config must be in targets file -->

‎src/Grpc.AspNetCore.Server.Reflection/GrpcReflectionServiceExtensions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#endregion
1818

19+
using System.Diagnostics.CodeAnalysis;
1920
using System.Reflection;
2021
using Grpc.AspNetCore.Server;
2122
using Grpc.Core;
@@ -120,6 +121,7 @@ public static IServiceCollection AddGrpcReflection(this IServiceCollection servi
120121
return null;
121122
}
122123

124+
[SuppressMessage("Trimming", "IL2075", Justification = "Modern apps published with trimming will be using newer Grpc.Tools and won't use this fallback.")]
123125
private static PropertyInfo? GetDescriptorPropertyFallback(Type serviceType)
124126
{
125127
// Search for the generated service base class

‎src/Grpc.AspNetCore.Server/GrpcMethodMetadata.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#endregion
1818

19+
using System.Diagnostics.CodeAnalysis;
20+
using Grpc.AspNetCore.Server.Internal;
1921
using Grpc.Core;
2022

2123
namespace Grpc.AspNetCore.Server
@@ -30,7 +32,12 @@ public sealed class GrpcMethodMetadata
3032
/// </summary>
3133
/// <param name="serviceType">The implementing service type.</param>
3234
/// <param name="method">The method representation.</param>
33-
public GrpcMethodMetadata(Type serviceType, IMethod method)
35+
public GrpcMethodMetadata(
36+
#if NET5_0_OR_GREATER
37+
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
38+
#endif
39+
Type serviceType,
40+
IMethod method)
3441
{
3542
if (serviceType == null)
3643
{
@@ -49,6 +56,9 @@ public GrpcMethodMetadata(Type serviceType, IMethod method)
4956
/// <summary>
5057
/// Gets the implementing service type.
5158
/// </summary>
59+
#if NET5_0_OR_GREATER
60+
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
61+
#endif
5262
public Type ServiceType { get; }
5363

5464
/// <summary>

‎src/Grpc.Core.Api/BindServiceMethodAttribute.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ namespace Grpc.Core
3030
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
3131
public class BindServiceMethodAttribute : Attribute
3232
{
33-
// grpc-dotnet uses reflection to find the bind service method.
34-
// DynamicallyAccessedMembersAttribute instructs the linker to never trim the method.
35-
private const DynamicallyAccessedMemberTypes ServiceBinderAccessibility = DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
33+
// Grpc.AspNetCore.Server uses reflection to find the bind service method.
34+
// Grpc.AspNetCore.Server.Reflection uses reflection to find the descriptor property.
35+
// DynamicallyAccessedMembersAttribute instructs the linker to never trim the members.
36+
private const DynamicallyAccessedMemberTypes ServiceBinderAccessibility = DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods | DynamicallyAccessedMemberTypes.PublicProperties;
3637

3738
/// <summary>
3839
/// Initializes a new instance of the <see cref="BindServiceMethodAttribute"/> class.

0 commit comments

Comments
 (0)
Please sign in to comment.