Skip to content

Commit

Permalink
Support aggressive trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Oct 17, 2020
1 parent 4827aea commit 5b89ace
Show file tree
Hide file tree
Showing 21 changed files with 803 additions and 8 deletions.
14 changes: 14 additions & 0 deletions Grpc.DotNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InteropTestsGrpcWebClient",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GrpcClient", "perf\benchmarkapps\GrpcClient\GrpcClient.csproj", "{D241B525-3B50-42EA-9E43-052745549BA6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkerTestsClient", "testassets\LinkerTestsClient\LinkerTestsClient.csproj", "{1FA0B07B-E65C-4380-94A4-75F10312487D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LinkerTestsWebsite", "testassets\LinkerTestsWebsite\LinkerTestsWebsite.csproj", "{FC5F9350-B812-4C62-AE76-0FF437F0F362}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -236,6 +240,14 @@ Global
{D241B525-3B50-42EA-9E43-052745549BA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D241B525-3B50-42EA-9E43-052745549BA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D241B525-3B50-42EA-9E43-052745549BA6}.Release|Any CPU.Build.0 = Release|Any CPU
{1FA0B07B-E65C-4380-94A4-75F10312487D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FA0B07B-E65C-4380-94A4-75F10312487D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FA0B07B-E65C-4380-94A4-75F10312487D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FA0B07B-E65C-4380-94A4-75F10312487D}.Release|Any CPU.Build.0 = Release|Any CPU
{FC5F9350-B812-4C62-AE76-0FF437F0F362}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC5F9350-B812-4C62-AE76-0FF437F0F362}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC5F9350-B812-4C62-AE76-0FF437F0F362}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC5F9350-B812-4C62-AE76-0FF437F0F362}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -278,6 +290,8 @@ Global
{AE2CF906-6C98-40F5-8EE5-3DBAC572F114} = {59C7B1F0-EE4D-4098-8596-0ADDBC305234}
{10DD7EEB-7B3A-4BF2-9562-78831FB06001} = {59C7B1F0-EE4D-4098-8596-0ADDBC305234}
{D241B525-3B50-42EA-9E43-052745549BA6} = {1B8B6117-CE39-4580-BAFA-D8026102767A}
{1FA0B07B-E65C-4380-94A4-75F10312487D} = {59C7B1F0-EE4D-4098-8596-0ADDBC305234}
{FC5F9350-B812-4C62-AE76-0FF437F0F362} = {59C7B1F0-EE4D-4098-8596-0ADDBC305234}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CD5C2B19-49B4-480A-990C-36D98A719B07}
Expand Down
2 changes: 1 addition & 1 deletion build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<BenchmarkDotNetPackageVersion>0.12.1</BenchmarkDotNetPackageVersion>
<GoogleProtobufPackageVersion>3.13.0</GoogleProtobufPackageVersion>
<GrpcDotNetPackageVersion>2.32.0</GrpcDotNetPackageVersion> <!-- Used by example projects -->
<GrpcPackageVersion>2.32.0</GrpcPackageVersion>
<GrpcPackageVersion>2.33.0-pre1</GrpcPackageVersion>
<MicrosoftAspNetCoreAppPackageVersion>5.0.0-rc.1.20451.17</MicrosoftAspNetCoreAppPackageVersion>
<MicrosoftAspNetCoreApp31PackageVersion>3.1.3</MicrosoftAspNetCoreApp31PackageVersion>
<MicrosoftBuildLocatorPackageVersion>1.2.2</MicrosoftBuildLocatorPackageVersion>
Expand Down
11 changes: 10 additions & 1 deletion src/Grpc.AspNetCore.Server/GrpcEndpointRouteBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#endregion

using System;
using System.Diagnostics.CodeAnalysis;
using Grpc.AspNetCore.Server.Internal;
using Grpc.AspNetCore.Server.Model.Internal;
using Microsoft.AspNetCore.Routing;
Expand All @@ -29,13 +30,21 @@ namespace Microsoft.AspNetCore.Builder
/// </summary>
public static class GrpcEndpointRouteBuilderExtensions
{
#if NET5_0
private const DynamicallyAccessedMemberTypes ServiceAccessibility = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
#endif

/// <summary>
/// Maps incoming requests to the specified <typeparamref name="TService"/> type.
/// </summary>
/// <typeparam name="TService">The service type to map requests to.</typeparam>
/// <param name="builder">The <see cref="IEndpointRouteBuilder"/> to add the route to.</param>
/// <returns>A <see cref="GrpcServiceEndpointConventionBuilder"/> for endpoints associated with the service.</returns>
public static GrpcServiceEndpointConventionBuilder MapGrpcService<TService>(this IEndpointRouteBuilder builder) where TService : class
public static GrpcServiceEndpointConventionBuilder MapGrpcService<
#if NET5_0
[DynamicallyAccessedMembers(ServiceAccessibility)]
#endif
TService>(this IEndpointRouteBuilder builder) where TService : class
{
if (builder == null)
{
Expand Down
13 changes: 11 additions & 2 deletions src/Grpc.AspNetCore.Server/InterceptorCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using Grpc.Core.Interceptors;

namespace Grpc.AspNetCore.Server
Expand All @@ -33,7 +34,11 @@ public class InterceptorCollection : Collection<InterceptorRegistration>
/// </summary>
/// <typeparam name="TInterceptor">The interceptor type.</typeparam>
/// <param name="args">The list of arguments to pass to the interceptor constructor when creating an instance.</param>
public void Add<TInterceptor>(params object[] args) where TInterceptor : Interceptor
public void Add<
#if NET5_0
[DynamicallyAccessedMembers(InterceptorRegistration.InterceptorAccessibility)]
#endif
TInterceptor>(params object[] args) where TInterceptor : Interceptor
{
Add(typeof(TInterceptor), args);
}
Expand All @@ -43,7 +48,11 @@ public void Add<TInterceptor>(params object[] args) where TInterceptor : Interce
/// </summary>
/// <param name="interceptorType">The interceptor type.</param>
/// <param name="args">The list of arguments to pass to the interceptor constructor when creating an instance.</param>
public void Add(Type interceptorType, params object[] args)
public void Add(
#if NET5_0
[DynamicallyAccessedMembers(InterceptorRegistration.InterceptorAccessibility)]
#endif
Type interceptorType, params object[] args)
{
if (interceptorType == null)
{
Expand Down
14 changes: 13 additions & 1 deletion src/Grpc.AspNetCore.Server/InterceptorRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Grpc.AspNetCore.Server.Internal;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -29,9 +30,17 @@ namespace Grpc.AspNetCore.Server
/// </summary>
public class InterceptorRegistration
{
#if NET5_0
internal const DynamicallyAccessedMemberTypes InterceptorAccessibility = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicMethods;
#endif

internal object[] _args;

internal InterceptorRegistration(Type type, object[] arguments)
internal InterceptorRegistration(
#if NET5_0
[DynamicallyAccessedMembers(InterceptorAccessibility)]
#endif
Type type, object[] arguments)
{
if (type == null)
{
Expand All @@ -56,6 +65,9 @@ internal InterceptorRegistration(Type type, object[] arguments)
/// <summary>
/// Get the type of the interceptor.
/// </summary>
#if NET5_0
[DynamicallyAccessedMembers(InterceptorAccessibility)]
#endif
public Type Type { get; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@
#endregion

using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace Grpc.AspNetCore.Server.Internal
{
internal sealed class DefaultGrpcServiceActivator<TGrpcService> : IGrpcServiceActivator<TGrpcService> where TGrpcService : class
internal sealed class DefaultGrpcServiceActivator<
#if NET5_0
[DynamicallyAccessedMembers(ServiceAccessibility)]
#endif
TGrpcService> : IGrpcServiceActivator<TGrpcService> where TGrpcService : class
{
#if NET5_0
internal const DynamicallyAccessedMemberTypes ServiceAccessibility = DynamicallyAccessedMemberTypes.PublicConstructors;
#endif
private static readonly Lazy<ObjectFactory> _objectFactory = new Lazy<ObjectFactory>(() => ActivatorUtilities.CreateFactory(typeof(TGrpcService), Type.EmptyTypes));

public GrpcActivatorHandle<TGrpcService> Create(IServiceProvider serviceProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
#endregion

using System;
using System.Diagnostics.CodeAnalysis;
using Grpc.Shared.Server;
using Microsoft.Extensions.Logging;

namespace Grpc.AspNetCore.Server.Model.Internal
{
internal class BinderServiceMethodProvider<TService> : IServiceMethodProvider<TService> where TService : class
internal class BinderServiceMethodProvider<
#if NET5_0
[DynamicallyAccessedMembers(ProviderServiceBinder<TService>.ServiceAccessibility)]
#endif
TService> : IServiceMethodProvider<TService> where TService : class
{
private readonly ILogger<BinderServiceMethodProvider<TService>> _logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Grpc.Core;
using Microsoft.AspNetCore.Routing;

namespace Grpc.AspNetCore.Server.Model.Internal
{
internal class ProviderServiceBinder<TService> : ServiceBinderBase where TService : class
internal class ProviderServiceBinder<
#if NET5_0
[DynamicallyAccessedMembers(ServiceAccessibility)]
#endif
TService> : ServiceBinderBase where TService : class
{
#if NET5_0
// Non-public methods is required by GetMethod overload that has a BindingFlags argument.
internal const DynamicallyAccessedMemberTypes ServiceAccessibility = DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
#endif

private readonly ServiceMethodProviderContext<TService> _context;
private readonly Type _declaringType;

Expand Down Expand Up @@ -101,6 +111,7 @@ public override void AddMethod<TRequest, TResponse>(Method<TRequest, TResponse>
Type? currentType = typeof(TService);
while (currentType != null)
{
// Specify binding flags explicitly because we don't want to match static methods.
var matchingMethod = currentType.GetMethod(
methodName,
BindingFlags.Public | BindingFlags.Instance,
Expand Down
5 changes: 5 additions & 0 deletions src/Shared/Server/BindMethodFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#endregion

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Grpc.Core;

Expand Down Expand Up @@ -65,6 +66,10 @@ internal static class BindMethodFinder
return null;
}

#if NET5_0
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern",
Justification = "Fallback doesn't have BindServiceMethodAttribute so can't be verified.")]
#endif
internal static MethodInfo? GetBindMethodFallback(Type serviceType)
{
// Search for the generated service base class
Expand Down
5 changes: 5 additions & 0 deletions test/FunctionalTests/Grpc.AspNetCore.FunctionalTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<None Update="server1.pfx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>ProjectDirectory</_Parameter1>
<_Parameter2>$(ProjectDir)</_Parameter2>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp3.1'">
Expand Down
122 changes: 122 additions & 0 deletions test/FunctionalTests/Linker/Helpers/DotNetProcess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;

namespace Grpc.AspNetCore.FunctionalTests.Linker.Helpers
{
public class DotNetProcess : IDisposable
{
private readonly TaskCompletionSource<object?> _exitedTcs;
private readonly StringBuilder _output;
private readonly object _outputLock = new object();

protected Process Process { get; }

public DotNetProcess()
{
_output = new StringBuilder();

Process = new Process();
Process.StartInfo = new ProcessStartInfo
{
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
FileName = @"dotnet"
};
Process.EnableRaisingEvents = true;
Process.Exited += Process_Exited;
Process.OutputDataReceived += Process_OutputDataReceived;
Process.ErrorDataReceived += Process_ErrorDataReceived;

_exitedTcs = new TaskCompletionSource<object?>(TaskCreationOptions.RunContinuationsAsynchronously);
}

public Task WaitForExitAsync() => _exitedTcs.Task;
public int ExitCode => Process.ExitCode;
public bool HasExited => Process.HasExited;

public void Start(string arguments)
{
Process.StartInfo.Arguments = arguments;
Process.Start();

Process.BeginOutputReadLine();
Process.BeginErrorReadLine();
}

public string GetOutput()
{
lock (_outputLock)
{
return _output.ToString();
}
}

private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
var data = e.Data;
if (data != null)
{
lock (_outputLock)
{
_output.AppendLine(data);
}
}
}

private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
var data = e.Data;
if (data != null)
{
lock (_outputLock)
{
_output.AppendLine("ERROR: " + data);
}
}
}

private void Process_Exited(object? sender, EventArgs e)
{
_exitedTcs.TrySetResult(null);
}

public void Dispose()
{
try
{
if (!Process.HasExited)
{
Process.Kill(entireProcessTree: true);
}
}
catch
{
// Ignore error
}

Process.Dispose();
}
}
}
Loading

0 comments on commit 5b89ace

Please sign in to comment.