Skip to content

Commit 96c06f4

Browse files
authored
Support aggressive trimming (#1060)
1 parent be77811 commit 96c06f4

19 files changed

+703
-8
lines changed

Grpc.DotNet.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InteropTestsGrpcWebClient",
118118
EndProject
119119
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GrpcClient", "perf\benchmarkapps\GrpcClient\GrpcClient.csproj", "{D241B525-3B50-42EA-9E43-052745549BA6}"
120120
EndProject
121+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkerTestsClient", "testassets\LinkerTestsClient\LinkerTestsClient.csproj", "{1FA0B07B-E65C-4380-94A4-75F10312487D}"
122+
EndProject
123+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkerTestsWebsite", "testassets\LinkerTestsWebsite\LinkerTestsWebsite.csproj", "{FC5F9350-B812-4C62-AE76-0FF437F0F362}"
124+
EndProject
121125
Global
122126
GlobalSection(SolutionConfigurationPlatforms) = preSolution
123127
Debug|Any CPU = Debug|Any CPU
@@ -236,6 +240,14 @@ Global
236240
{D241B525-3B50-42EA-9E43-052745549BA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
237241
{D241B525-3B50-42EA-9E43-052745549BA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
238242
{D241B525-3B50-42EA-9E43-052745549BA6}.Release|Any CPU.Build.0 = Release|Any CPU
243+
{1FA0B07B-E65C-4380-94A4-75F10312487D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
244+
{1FA0B07B-E65C-4380-94A4-75F10312487D}.Debug|Any CPU.Build.0 = Debug|Any CPU
245+
{1FA0B07B-E65C-4380-94A4-75F10312487D}.Release|Any CPU.ActiveCfg = Release|Any CPU
246+
{1FA0B07B-E65C-4380-94A4-75F10312487D}.Release|Any CPU.Build.0 = Release|Any CPU
247+
{FC5F9350-B812-4C62-AE76-0FF437F0F362}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
248+
{FC5F9350-B812-4C62-AE76-0FF437F0F362}.Debug|Any CPU.Build.0 = Debug|Any CPU
249+
{FC5F9350-B812-4C62-AE76-0FF437F0F362}.Release|Any CPU.ActiveCfg = Release|Any CPU
250+
{FC5F9350-B812-4C62-AE76-0FF437F0F362}.Release|Any CPU.Build.0 = Release|Any CPU
239251
EndGlobalSection
240252
GlobalSection(SolutionProperties) = preSolution
241253
HideSolutionNode = FALSE
@@ -278,6 +290,8 @@ Global
278290
{AE2CF906-6C98-40F5-8EE5-3DBAC572F114} = {59C7B1F0-EE4D-4098-8596-0ADDBC305234}
279291
{10DD7EEB-7B3A-4BF2-9562-78831FB06001} = {59C7B1F0-EE4D-4098-8596-0ADDBC305234}
280292
{D241B525-3B50-42EA-9E43-052745549BA6} = {1B8B6117-CE39-4580-BAFA-D8026102767A}
293+
{1FA0B07B-E65C-4380-94A4-75F10312487D} = {59C7B1F0-EE4D-4098-8596-0ADDBC305234}
294+
{FC5F9350-B812-4C62-AE76-0FF437F0F362} = {59C7B1F0-EE4D-4098-8596-0ADDBC305234}
281295
EndGlobalSection
282296
GlobalSection(ExtensibilityGlobals) = postSolution
283297
SolutionGuid = {CD5C2B19-49B4-480A-990C-36D98A719B07}

build/dependencies.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<BenchmarkDotNetPackageVersion>0.12.1</BenchmarkDotNetPackageVersion>
44
<GoogleProtobufPackageVersion>3.13.0</GoogleProtobufPackageVersion>
55
<GrpcDotNetPackageVersion>2.32.0</GrpcDotNetPackageVersion> <!-- Used by example projects -->
6-
<GrpcPackageVersion>2.32.0</GrpcPackageVersion>
6+
<GrpcPackageVersion>2.33.0-pre1</GrpcPackageVersion>
77
<MicrosoftAspNetCoreAppPackageVersion>5.0.0-rc.1.20451.17</MicrosoftAspNetCoreAppPackageVersion>
88
<MicrosoftAspNetCoreApp31PackageVersion>3.1.3</MicrosoftAspNetCoreApp31PackageVersion>
99
<MicrosoftBuildLocatorPackageVersion>1.2.2</MicrosoftBuildLocatorPackageVersion>

src/Grpc.AspNetCore.Server/GrpcEndpointRouteBuilderExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#endregion
1818

1919
using System;
20+
using System.Diagnostics.CodeAnalysis;
2021
using Grpc.AspNetCore.Server.Internal;
2122
using Grpc.AspNetCore.Server.Model.Internal;
2223
using Microsoft.AspNetCore.Routing;
@@ -29,13 +30,21 @@ namespace Microsoft.AspNetCore.Builder
2930
/// </summary>
3031
public static class GrpcEndpointRouteBuilderExtensions
3132
{
33+
#if NET5_0
34+
private const DynamicallyAccessedMemberTypes ServiceAccessibility = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
35+
#endif
36+
3237
/// <summary>
3338
/// Maps incoming requests to the specified <typeparamref name="TService"/> type.
3439
/// </summary>
3540
/// <typeparam name="TService">The service type to map requests to.</typeparam>
3641
/// <param name="builder">The <see cref="IEndpointRouteBuilder"/> to add the route to.</param>
3742
/// <returns>A <see cref="GrpcServiceEndpointConventionBuilder"/> for endpoints associated with the service.</returns>
38-
public static GrpcServiceEndpointConventionBuilder MapGrpcService<TService>(this IEndpointRouteBuilder builder) where TService : class
43+
public static GrpcServiceEndpointConventionBuilder MapGrpcService<
44+
#if NET5_0
45+
[DynamicallyAccessedMembers(ServiceAccessibility)]
46+
#endif
47+
TService>(this IEndpointRouteBuilder builder) where TService : class
3948
{
4049
if (builder == null)
4150
{

src/Grpc.AspNetCore.Server/InterceptorCollection.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System;
2020
using System.Collections.Generic;
2121
using System.Collections.ObjectModel;
22+
using System.Diagnostics.CodeAnalysis;
2223
using Grpc.Core.Interceptors;
2324

2425
namespace Grpc.AspNetCore.Server
@@ -33,7 +34,11 @@ public class InterceptorCollection : Collection<InterceptorRegistration>
3334
/// </summary>
3435
/// <typeparam name="TInterceptor">The interceptor type.</typeparam>
3536
/// <param name="args">The list of arguments to pass to the interceptor constructor when creating an instance.</param>
36-
public void Add<TInterceptor>(params object[] args) where TInterceptor : Interceptor
37+
public void Add<
38+
#if NET5_0
39+
[DynamicallyAccessedMembers(InterceptorRegistration.InterceptorAccessibility)]
40+
#endif
41+
TInterceptor>(params object[] args) where TInterceptor : Interceptor
3742
{
3843
Add(typeof(TInterceptor), args);
3944
}
@@ -43,7 +48,11 @@ public void Add<TInterceptor>(params object[] args) where TInterceptor : Interce
4348
/// </summary>
4449
/// <param name="interceptorType">The interceptor type.</param>
4550
/// <param name="args">The list of arguments to pass to the interceptor constructor when creating an instance.</param>
46-
public void Add(Type interceptorType, params object[] args)
51+
public void Add(
52+
#if NET5_0
53+
[DynamicallyAccessedMembers(InterceptorRegistration.InterceptorAccessibility)]
54+
#endif
55+
Type interceptorType, params object[] args)
4756
{
4857
if (interceptorType == null)
4958
{

src/Grpc.AspNetCore.Server/InterceptorRegistration.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Diagnostics.CodeAnalysis;
2122
using System.Linq;
2223
using Grpc.AspNetCore.Server.Internal;
2324
using Microsoft.Extensions.DependencyInjection;
@@ -29,9 +30,17 @@ namespace Grpc.AspNetCore.Server
2930
/// </summary>
3031
public class InterceptorRegistration
3132
{
33+
#if NET5_0
34+
internal const DynamicallyAccessedMemberTypes InterceptorAccessibility = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicMethods;
35+
#endif
36+
3237
internal object[] _args;
3338

34-
internal InterceptorRegistration(Type type, object[] arguments)
39+
internal InterceptorRegistration(
40+
#if NET5_0
41+
[DynamicallyAccessedMembers(InterceptorAccessibility)]
42+
#endif
43+
Type type, object[] arguments)
3544
{
3645
if (type == null)
3746
{
@@ -56,6 +65,9 @@ internal InterceptorRegistration(Type type, object[] arguments)
5665
/// <summary>
5766
/// Get the type of the interceptor.
5867
/// </summary>
68+
#if NET5_0
69+
[DynamicallyAccessedMembers(InterceptorAccessibility)]
70+
#endif
5971
public Type Type { get; }
6072

6173
/// <summary>

src/Grpc.AspNetCore.Server/Internal/DefaultGrpcServiceActivator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@
1717
#endregion
1818

1919
using System;
20+
using System.Diagnostics.CodeAnalysis;
2021
using System.Threading.Tasks;
2122
using Microsoft.Extensions.DependencyInjection;
2223

2324
namespace Grpc.AspNetCore.Server.Internal
2425
{
25-
internal sealed class DefaultGrpcServiceActivator<TGrpcService> : IGrpcServiceActivator<TGrpcService> where TGrpcService : class
26+
internal sealed class DefaultGrpcServiceActivator<
27+
#if NET5_0
28+
[DynamicallyAccessedMembers(ServiceAccessibility)]
29+
#endif
30+
TGrpcService> : IGrpcServiceActivator<TGrpcService> where TGrpcService : class
2631
{
32+
#if NET5_0
33+
internal const DynamicallyAccessedMemberTypes ServiceAccessibility = DynamicallyAccessedMemberTypes.PublicConstructors;
34+
#endif
2735
private static readonly Lazy<ObjectFactory> _objectFactory = new Lazy<ObjectFactory>(() => ActivatorUtilities.CreateFactory(typeof(TGrpcService), Type.EmptyTypes));
2836

2937
public GrpcActivatorHandle<TGrpcService> Create(IServiceProvider serviceProvider)

src/Grpc.AspNetCore.Server/Model/Internal/BinderServiceModelProvider.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
#endregion
1818

1919
using System;
20+
using System.Diagnostics.CodeAnalysis;
2021
using Grpc.Shared.Server;
2122
using Microsoft.Extensions.Logging;
2223

2324
namespace Grpc.AspNetCore.Server.Model.Internal
2425
{
25-
internal class BinderServiceMethodProvider<TService> : IServiceMethodProvider<TService> where TService : class
26+
internal class BinderServiceMethodProvider<
27+
#if NET5_0
28+
[DynamicallyAccessedMembers(ProviderServiceBinder<TService>.ServiceAccessibility)]
29+
#endif
30+
TService> : IServiceMethodProvider<TService> where TService : class
2631
{
2732
private readonly ILogger<BinderServiceMethodProvider<TService>> _logger;
2833

src/Grpc.AspNetCore.Server/Model/Internal/ProviderServiceBinder.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,24 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Diagnostics.CodeAnalysis;
2122
using System.Reflection;
2223
using Grpc.Core;
2324
using Microsoft.AspNetCore.Routing;
2425

2526
namespace Grpc.AspNetCore.Server.Model.Internal
2627
{
27-
internal class ProviderServiceBinder<TService> : ServiceBinderBase where TService : class
28+
internal class ProviderServiceBinder<
29+
#if NET5_0
30+
[DynamicallyAccessedMembers(ServiceAccessibility)]
31+
#endif
32+
TService> : ServiceBinderBase where TService : class
2833
{
34+
#if NET5_0
35+
// Non-public methods is required by GetMethod overload that has a BindingFlags argument.
36+
internal const DynamicallyAccessedMemberTypes ServiceAccessibility = DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
37+
#endif
38+
2939
private readonly ServiceMethodProviderContext<TService> _context;
3040
private readonly Type _declaringType;
3141

@@ -101,6 +111,7 @@ public override void AddMethod<TRequest, TResponse>(Method<TRequest, TResponse>
101111
Type? currentType = typeof(TService);
102112
while (currentType != null)
103113
{
114+
// Specify binding flags explicitly because we don't want to match static methods.
104115
var matchingMethod = currentType.GetMethod(
105116
methodName,
106117
BindingFlags.Public | BindingFlags.Instance,

src/Shared/Server/BindMethodFinder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#endregion
1818

1919
using System;
20+
using System.Diagnostics.CodeAnalysis;
2021
using System.Reflection;
2122
using Grpc.Core;
2223

@@ -65,6 +66,10 @@ internal static class BindMethodFinder
6566
return null;
6667
}
6768

69+
#if NET5_0
70+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern",
71+
Justification = "Fallback doesn't have BindServiceMethodAttribute so can't be verified.")]
72+
#endif
6873
internal static MethodInfo? GetBindMethodFallback(Type serviceType)
6974
{
7075
// Search for the generated service base class

test/FunctionalTests/Grpc.AspNetCore.FunctionalTests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
<None Update="server1.pfx">
3030
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3131
</None>
32+
33+
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
34+
<_Parameter1>ProjectDirectory</_Parameter1>
35+
<_Parameter2>$(ProjectDir)</_Parameter2>
36+
</AssemblyAttribute>
3237
</ItemGroup>
3338

3439
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">

0 commit comments

Comments
 (0)