Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix server trim error from dependency injection #1471

Merged
merged 3 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ namespace Microsoft.AspNetCore.Builder
/// </summary>
public static class GrpcEndpointRouteBuilderExtensions
{
#if NET5_0_OR_GREATER
private const DynamicallyAccessedMemberTypes ServiceAccessibility = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
#endif

/// <summary>
/// Maps incoming requests to the specified <typeparamref name="TService"/> type.
/// </summary>
Expand All @@ -42,7 +38,7 @@ public static class GrpcEndpointRouteBuilderExtensions
/// <returns>A <see cref="GrpcServiceEndpointConventionBuilder"/> for endpoints associated with the service.</returns>
public static GrpcServiceEndpointConventionBuilder MapGrpcService<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(ServiceAccessibility)]
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService>(this IEndpointRouteBuilder builder) where TService : class
{
Expand Down
8 changes: 7 additions & 1 deletion src/Grpc.AspNetCore.Server/IGrpcServiceActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
#endregion

using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Grpc.AspNetCore.Server.Internal;

namespace Grpc.AspNetCore.Server
{
/// <summary>
/// A <typeparamref name="TGrpcService"/> activator abstraction.
/// </summary>
/// <typeparam name="TGrpcService">The service type.</typeparam>
public interface IGrpcServiceActivator<TGrpcService> where TGrpcService : class
public interface IGrpcServiceActivator<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TGrpcService> where TGrpcService : class
{
/// <summary>
/// Creates a service.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#endregion

using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Shared.Server;
Expand All @@ -24,7 +25,11 @@

namespace Grpc.AspNetCore.Server.Internal.CallHandlers
{
internal class ClientStreamingServerCallHandler<TService, TRequest, TResponse> : ServerCallHandlerBase<TService, TRequest, TResponse>
internal class ClientStreamingServerCallHandler<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService, TRequest, TResponse> : ServerCallHandlerBase<TService, TRequest, TResponse>
where TRequest : class
where TResponse : class
where TService : class
Expand Down
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.Threading.Tasks;
using Grpc.AspNetCore.Server.Model;
using Grpc.Core;
Expand All @@ -27,7 +28,11 @@

namespace Grpc.AspNetCore.Server.Internal.CallHandlers
{
internal class DuplexStreamingServerCallHandler<TService, TRequest, TResponse> : ServerCallHandlerBase<TService, TRequest, TResponse>
internal class DuplexStreamingServerCallHandler<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService, TRequest, TResponse> : ServerCallHandlerBase<TService, TRequest, TResponse>
where TRequest : class
where TResponse : class
where TService : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#endregion

using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Grpc.AspNetCore.Server.Model;
using Grpc.Core;
using Grpc.Shared.Server;
using Microsoft.AspNetCore.Http;
Expand All @@ -29,7 +29,11 @@

namespace Grpc.AspNetCore.Server.Internal.CallHandlers
{
internal abstract class ServerCallHandlerBase<TService, TRequest, TResponse>
internal abstract class ServerCallHandlerBase<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService, TRequest, TResponse>
where TService : class
where TRequest : class
where TResponse : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@

#endregion

using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Grpc.AspNetCore.Server.Model;
using Grpc.Core;
using Grpc.Shared.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Grpc.AspNetCore.Server.Internal.CallHandlers
{
internal class ServerStreamingServerCallHandler<TService, TRequest, TResponse> : ServerCallHandlerBase<TService, TRequest, TResponse>
internal class ServerStreamingServerCallHandler<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService, TRequest, TResponse> : ServerCallHandlerBase<TService, TRequest, TResponse>
where TRequest : class
where TResponse : class
where TService : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@

#endregion

using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Grpc.AspNetCore.Server.Model;
using Grpc.Core;
using Grpc.Shared.Server;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Grpc.AspNetCore.Server.Internal.CallHandlers
{
internal class UnaryServerCallHandler<TService, TRequest, TResponse> : ServerCallHandlerBase<TService, TRequest, TResponse>
internal class UnaryServerCallHandler<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService, TRequest, TResponse> : ServerCallHandlerBase<TService, TRequest, TResponse>
where TRequest : class
where TResponse : class
where TService : class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ namespace Grpc.AspNetCore.Server.Internal
{
internal sealed class DefaultGrpcServiceActivator<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(ServiceAccessibility)]
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TGrpcService> : IGrpcServiceActivator<TGrpcService> where TGrpcService : class
{
#if NET5_0_OR_GREATER
internal const DynamicallyAccessedMemberTypes ServiceAccessibility = DynamicallyAccessedMemberTypes.PublicConstructors;
#endif
private static readonly Lazy<ObjectFactory> _objectFactory = new Lazy<ObjectFactory>(static () => ActivatorUtilities.CreateFactory(typeof(TGrpcService), Type.EmptyTypes));

public GrpcActivatorHandle<TGrpcService> Create(IServiceProvider serviceProvider)
Expand Down
5 changes: 5 additions & 0 deletions src/Grpc.AspNetCore.Server/Internal/GrpcProtocolConstants.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.Runtime.CompilerServices;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
Expand Down Expand Up @@ -127,5 +128,9 @@ internal static int GetCancelErrorCode(string protocol)
return Http2ResetStreamCancel;
#endif
}

#if NET5_0_OR_GREATER
internal const DynamicallyAccessedMemberTypes ServiceAccessibility = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
#endif
}
}
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.Threading.Tasks;
using Grpc.AspNetCore.Server.Internal.CallHandlers;
using Grpc.AspNetCore.Server.Model;
Expand All @@ -32,7 +33,11 @@ namespace Grpc.AspNetCore.Server.Internal
/// <summary>
/// Creates server call handlers. Provides a place to get services that call handlers will use.
/// </summary>
internal partial class ServerCallHandlerFactory<TService> where TService : class
internal partial class ServerCallHandlerFactory<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService> where TService : class
{
private readonly ILoggerFactory _loggerFactory;
private readonly IGrpcServiceActivator<TService> _serviceActivator;
Expand Down
9 changes: 8 additions & 1 deletion src/Grpc.AspNetCore.Server/Model/IServiceMethodProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

#endregion

using System.Diagnostics.CodeAnalysis;
using Grpc.AspNetCore.Server.Internal;

namespace Grpc.AspNetCore.Server.Model
{
/// <summary>
Expand All @@ -28,7 +31,11 @@ namespace Grpc.AspNetCore.Server.Model
/// <see cref="IServiceMethodProvider{TService}"/> instances are invoked in the order they are registered.
/// </para>
/// </remarks>
public interface IServiceMethodProvider<TService> where TService : class
public interface IServiceMethodProvider<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService> where TService : class
{
/// <summary>
/// Called to execute the provider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Diagnostics.CodeAnalysis;
using Grpc.AspNetCore.Server.Internal;
using Grpc.Shared.Server;
using Microsoft.Extensions.Logging;
using Log = Grpc.AspNetCore.Server.Model.Internal.BinderServiceMethodProviderLog;
Expand All @@ -26,7 +27,7 @@ namespace Grpc.AspNetCore.Server.Model.Internal
{
internal class BinderServiceMethodProvider<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(ProviderServiceBinder<TService>.ServiceAccessibility)]
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService> : IServiceMethodProvider<TService> where TService : class
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,18 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Grpc.AspNetCore.Server.Internal;
using Grpc.Core;
using Microsoft.AspNetCore.Routing;

namespace Grpc.AspNetCore.Server.Model.Internal
{
internal class ProviderServiceBinder<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(ServiceAccessibility)]
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService> : ServiceBinderBase where TService : class
{
#if NET5_0_OR_GREATER
// 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
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 Grpc.Core;
Expand All @@ -31,7 +32,11 @@

namespace Grpc.AspNetCore.Server.Model.Internal
{
internal class ServiceRouteBuilder<TService> where TService : class
internal class ServiceRouteBuilder<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService> where TService : class
{
private readonly IEnumerable<IServiceMethodProvider<TService>> _serviceMethodProviders;
private readonly ServerCallHandlerFactory<TService> _serverCallHandlerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#endregion

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Grpc.AspNetCore.Server.Internal;
using Grpc.AspNetCore.Server.Model.Internal;
using Grpc.Core;
Expand All @@ -29,7 +30,11 @@ namespace Grpc.AspNetCore.Server.Model
/// A context for <see cref="IServiceMethodProvider{TService}"/>.
/// </summary>
/// <typeparam name="TService">Service type for the context.</typeparam>
public class ServiceMethodProviderContext<TService> where TService : class
public class ServiceMethodProviderContext<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService> where TService : class
{
private readonly ServerCallHandlerFactory<TService> _serverCallHandlerFactory;

Expand Down
8 changes: 7 additions & 1 deletion src/Shared/Server/ClientStreamingServerMethodInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

#endregion

using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Grpc.AspNetCore.Server;
using Grpc.AspNetCore.Server.Internal;
using Grpc.AspNetCore.Server.Model;
using Grpc.Core;
using Microsoft.AspNetCore.Http;
Expand All @@ -30,7 +32,11 @@ namespace Grpc.Shared.Server
/// <typeparam name="TService">Service type for this method.</typeparam>
/// <typeparam name="TRequest">Request message type for this method.</typeparam>
/// <typeparam name="TResponse">Response message type for this method.</typeparam>
internal sealed class ClientStreamingServerMethodInvoker<TService, TRequest, TResponse> : ServerMethodInvokerBase<TService, TRequest, TResponse>
internal sealed class ClientStreamingServerMethodInvoker<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService, TRequest, TResponse> : ServerMethodInvokerBase<TService, TRequest, TResponse>
where TRequest : class
where TResponse : class
where TService : class
Expand Down
8 changes: 7 additions & 1 deletion src/Shared/Server/DuplexStreamingServerMethodInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

#endregion

using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Grpc.AspNetCore.Server;
using Grpc.AspNetCore.Server.Internal;
using Grpc.AspNetCore.Server.Model;
using Grpc.Core;
using Microsoft.AspNetCore.Http;
Expand All @@ -30,7 +32,11 @@ namespace Grpc.Shared.Server
/// <typeparam name="TService">Service type for this method.</typeparam>
/// <typeparam name="TRequest">Request message type for this method.</typeparam>
/// <typeparam name="TResponse">Response message type for this method.</typeparam>
internal sealed class DuplexStreamingServerMethodInvoker<TService, TRequest, TResponse> : ServerMethodInvokerBase<TService, TRequest, TResponse>
internal sealed class DuplexStreamingServerMethodInvoker<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService, TRequest, TResponse> : ServerMethodInvokerBase<TService, TRequest, TResponse>
where TRequest : class
where TResponse : class
where TService : class
Expand Down
8 changes: 7 additions & 1 deletion src/Shared/Server/ServerMethodInvokerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

#endregion

using System.Diagnostics.CodeAnalysis;
using Grpc.AspNetCore.Server;
using Grpc.AspNetCore.Server.Internal;
using Grpc.Core;

namespace Grpc.Shared.Server
Expand All @@ -27,7 +29,11 @@ namespace Grpc.Shared.Server
/// <typeparam name="TService">Service type for this method.</typeparam>
/// <typeparam name="TRequest">Request message type for this method.</typeparam>
/// <typeparam name="TResponse">Response message type for this method.</typeparam>
internal abstract class ServerMethodInvokerBase<TService, TRequest, TResponse>
internal abstract class ServerMethodInvokerBase<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(GrpcProtocolConstants.ServiceAccessibility)]
#endif
TService, TRequest, TResponse>
where TRequest : class
where TResponse : class
where TService : class
Expand Down
Loading