Skip to content

Commit c24dfeb

Browse files
authored
Fix .NET 6 trimming constraint constructor (#2080)
1 parent 95cf7fc commit c24dfeb

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/Grpc.AspNetCore.Server/Grpc.AspNetCore.Server.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>gRPC support for ASP.NET Core</Description>
@@ -29,6 +29,7 @@
2929
<Compile Include="..\Shared\Server\ServerStreamingServerMethodInvoker.cs" Link="Model\Internal\ServerStreamingServerMethodInvoker.cs" />
3030
<Compile Include="..\Shared\Server\UnaryServerMethodInvoker.cs" Link="Model\Internal\UnaryServerMethodInvoker.cs" />
3131
<Compile Include="..\Shared\NullableAttributes.cs" Link="Internal\NullableAttributes.cs" />
32+
<Compile Include="..\Shared\CodeAnalysisAttributes.cs" Link="Internal\CodeAnalysisAttributes.cs" />
3233
</ItemGroup>
3334

3435
<ItemGroup>

src/Grpc.AspNetCore.Server/GrpcServiceExtensions.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
#endregion
1818

19+
using System.Diagnostics.CodeAnalysis;
1920
using Grpc.AspNetCore.Server;
2021
using Grpc.AspNetCore.Server.Internal;
2122
using Grpc.AspNetCore.Server.Model;
2223
using Grpc.AspNetCore.Server.Model.Internal;
24+
using Microsoft.AspNetCore.Routing;
2325
using Microsoft.Extensions.DependencyInjection.Extensions;
2426
using Microsoft.Extensions.Options;
2527

@@ -65,11 +67,7 @@ public static IGrpcServerBuilder AddGrpc(this IServiceCollection services)
6567
{
6668
// Unimplemented constraint is added to the route as an inline constraint to avoid RoutePatternFactory.Parse overload that includes parameter policies. That overload infers strings as regex constraints, which brings in
6769
// the regex engine when publishing trimmed or AOT apps. This change reduces Native AOT gRPC server app size by about 1 MB.
68-
#if NET7_0_OR_GREATER
69-
options.SetParameterPolicy<GrpcUnimplementedConstraint>(GrpcServerConstants.GrpcUnimplementedConstraintPrefix);
70-
#else
71-
options.ConstraintMap[GrpcServerConstants.GrpcUnimplementedConstraintPrefix] = typeof(GrpcUnimplementedConstraint);
72-
#endif
70+
AddParameterPolicy<GrpcUnimplementedConstraint>(options, GrpcServerConstants.GrpcUnimplementedConstraintPrefix);
7371
});
7472
services.AddOptions();
7573
services.TryAddSingleton<GrpcMarkerService>();
@@ -84,6 +82,17 @@ public static IGrpcServerBuilder AddGrpc(this IServiceCollection services)
8482
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IServiceMethodProvider<>), typeof(BinderServiceMethodProvider<>)));
8583

8684
return new GrpcServerBuilder(services);
85+
86+
// This ensures the policy's constructors are preserved in .NET 6 with trimming. Remove when .NET 6 is no longer supported.
87+
static void AddParameterPolicy<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(RouteOptions options, string name)
88+
where T : IParameterPolicy
89+
{
90+
#if NET7_0_OR_GREATER
91+
options.SetParameterPolicy<T>(name);
92+
#else
93+
options.ConstraintMap[name] = typeof(T);
94+
#endif
95+
}
8796
}
8897

8998
/// <summary>

src/Grpc.Core.Api/Grpc.Core.Api.csproj

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

1717
<ItemGroup>
1818
<None Include="README.md" Pack="true" PackagePath="\" />
19+
20+
<Compile Include="..\Shared\CodeAnalysisAttributes.cs" Link="Internal\CodeAnalysisAttributes.cs" />
1921
</ItemGroup>
2022

2123
<ItemGroup>

0 commit comments

Comments
 (0)