Skip to content
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
8 changes: 7 additions & 1 deletion src/Grpc.Net.ClientFactory/GrpcClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#endregion

using System.Diagnostics.CodeAnalysis;

namespace Grpc.Net.ClientFactory
{
/// <summary>
Expand All @@ -30,6 +32,10 @@ public abstract class GrpcClientFactory
/// <typeparam name="TClient">The gRPC client type.</typeparam>
/// <param name="name">The configuration name.</param>
/// <returns>A gRPC client instance.</returns>
public abstract TClient CreateClient<TClient>(string name) where TClient : class;
public abstract TClient CreateClient<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
TClient>(string name) where TClient : class;
}
}
13 changes: 11 additions & 2 deletions src/Grpc.Net.ClientFactory/GrpcClientServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#endregion

using System.Diagnostics.CodeAnalysis;
using Grpc.Net.ClientFactory;
using Grpc.Net.ClientFactory.Internal;
using Grpc.Shared;
Expand Down Expand Up @@ -280,7 +281,11 @@ public static IHttpClientBuilder AddGrpcClient<TClient>(this IServiceCollection
return services.AddGrpcClientCore<TClient>(name);
}

private static IHttpClientBuilder AddGrpcClientCore<TClient>(this IServiceCollection services, string name) where TClient : class
private static IHttpClientBuilder AddGrpcClientCore<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
TClient>(this IServiceCollection services, string name) where TClient : class
{
if (name == null)
{
Expand All @@ -305,7 +310,11 @@ private static IHttpClientBuilder AddGrpcClientCore<TClient>(this IServiceCollec
/// <summary>
/// This is a custom method to register the HttpClient and typed factory. Needed because we need to access the config name when creating the typed client
/// </summary>
private static IHttpClientBuilder AddGrpcHttpClient<TClient>(this IServiceCollection services, string name)
private static IHttpClientBuilder AddGrpcHttpClient<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
TClient>(this IServiceCollection services, string name)
where TClient : class
{
if (services == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@

#endregion

using System.Diagnostics.CodeAnalysis;
using Grpc.Core;
using Microsoft.Extensions.DependencyInjection;

namespace Grpc.Net.ClientFactory.Internal
{
// Should be registered as a singleton, so it that it can act as a cache for the Activator.
internal class DefaultClientActivator<TClient> where TClient : class
internal class DefaultClientActivator<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
TClient> where TClient : class
{
private static readonly Func<ObjectFactory> _createActivator = static () => ActivatorUtilities.CreateFactory(typeof(TClient), new Type[] { typeof(CallInvoker), });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#endregion

using System.Diagnostics.CodeAnalysis;
using Grpc.Core;
using Grpc.Core.Interceptors;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -38,7 +39,11 @@ public DefaultGrpcClientFactory(IServiceProvider serviceProvider,
_grpcClientFactoryOptionsMonitor = grpcClientFactoryOptionsMonitor;
}

public override TClient CreateClient<TClient>(string name) where TClient : class
public override TClient CreateClient<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
TClient>(string name) where TClient : class
{
var defaultClientActivator = _serviceProvider.GetService<DefaultClientActivator<TClient>>();
if (defaultClientActivator == null)
Expand Down