Skip to content

Commit df96209

Browse files
committed
Fix tests
1 parent cceafcc commit df96209

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/Grpc.AspNetCore.Server/GrpcServiceExtensions.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ public static IGrpcServerBuilder AddGrpc(this IServiceCollection services)
5858
{
5959
ArgumentNullThrowHelper.ThrowIfNull(services);
6060

61-
// Configure route options directly. Depend on the host to call AddRouting/AddRoutingCore.
62-
services.Configure<RouteOptions>(options =>
63-
{
64-
// 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
65-
// the regex engine when publishing trimmed or AOT apps. This change reduces Native AOT gRPC server app size by about 1 MB.
66-
AddParameterPolicy<GrpcUnimplementedConstraint>(options, GrpcServerConstants.GrpcUnimplementedConstraintPrefix);
67-
});
61+
#if NET8_0_OR_GREATER
62+
// Prefer AddRoutingCore when available.
63+
// AddRoutingCore doesn't register a regex constraint and produces smaller result from trimming.
64+
services.AddRoutingCore();
65+
services.Configure<RouteOptions>(ConfigureRouting);
66+
#else
67+
services.AddRouting(ConfigureRouting);
68+
#endif
6869
services.AddOptions();
6970
services.TryAddSingleton<GrpcMarkerService>();
7071
services.TryAddSingleton(typeof(ServerCallHandlerFactory<>));
@@ -79,6 +80,13 @@ public static IGrpcServerBuilder AddGrpc(this IServiceCollection services)
7980

8081
return new GrpcServerBuilder(services);
8182

83+
static void ConfigureRouting(RouteOptions options)
84+
{
85+
// 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
86+
// the regex engine when publishing trimmed or AOT apps. This change reduces Native AOT gRPC server app size by about 1 MB.
87+
AddParameterPolicy<GrpcUnimplementedConstraint>(options, GrpcServerConstants.GrpcUnimplementedConstraintPrefix);
88+
}
89+
8290
// This ensures the policy's constructors are preserved in .NET 6 with trimming. Remove when .NET 6 is no longer supported.
8391
static void AddParameterPolicy<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(RouteOptions options, string name)
8492
where T : IParameterPolicy

testassets/LinkerTestsClient/LinkerTestsClient.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<PublishTrimmed>true</PublishTrimmed>
77
<PublishAot>$(AppPublishAot)</PublishAot>
88

9-
<IlcGenerateMstatFile>true</IlcGenerateMstatFile>
10-
<IlcGenerateDgmlFile>true</IlcGenerateDgmlFile>
9+
<IlcGenerateMstatFile>$(GenerateAotDiaganostics)</IlcGenerateMstatFile>
10+
<IlcGenerateDgmlFile>$(GenerateAotDiaganostics)</IlcGenerateDgmlFile>
1111
</PropertyGroup>
1212

1313
<ItemGroup>

testassets/LinkerTestsWebsite/LinkerTestsWebsite.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<TrimMode>full</TrimMode>
1313
<TrimmerSingleWarn>false</TrimmerSingleWarn>
1414

15-
<IlcGenerateMstatFile>true</IlcGenerateMstatFile>
16-
<IlcGenerateDgmlFile>true</IlcGenerateDgmlFile>
15+
<IlcGenerateMstatFile>$(GenerateAotDiaganostics)</IlcGenerateMstatFile>
16+
<IlcGenerateDgmlFile>$(GenerateAotDiaganostics)</IlcGenerateDgmlFile>
1717
</PropertyGroup>
1818

1919
<ItemGroup>

0 commit comments

Comments
 (0)