Skip to content

Commit a984c6d

Browse files
authored
Fix client factory trim warning (#1806)
1 parent 7a6e2fe commit a984c6d

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

src/Grpc.Net.ClientFactory/GrpcClientFactory.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#endregion
1818

19+
using System.Diagnostics.CodeAnalysis;
20+
1921
namespace Grpc.Net.ClientFactory
2022
{
2123
/// <summary>
@@ -30,6 +32,10 @@ public abstract class GrpcClientFactory
3032
/// <typeparam name="TClient">The gRPC client type.</typeparam>
3133
/// <param name="name">The configuration name.</param>
3234
/// <returns>A gRPC client instance.</returns>
33-
public abstract TClient CreateClient<TClient>(string name) where TClient : class;
35+
public abstract TClient CreateClient<
36+
#if NET5_0_OR_GREATER
37+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
38+
#endif
39+
TClient>(string name) where TClient : class;
3440
}
3541
}

src/Grpc.Net.ClientFactory/GrpcClientServiceExtensions.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#endregion
1818

19+
using System.Diagnostics.CodeAnalysis;
1920
using Grpc.Net.ClientFactory;
2021
using Grpc.Net.ClientFactory.Internal;
2122
using Grpc.Shared;
@@ -280,7 +281,11 @@ public static IHttpClientBuilder AddGrpcClient<TClient>(this IServiceCollection
280281
return services.AddGrpcClientCore<TClient>(name);
281282
}
282283

283-
private static IHttpClientBuilder AddGrpcClientCore<TClient>(this IServiceCollection services, string name) where TClient : class
284+
private static IHttpClientBuilder AddGrpcClientCore<
285+
#if NET5_0_OR_GREATER
286+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
287+
#endif
288+
TClient>(this IServiceCollection services, string name) where TClient : class
284289
{
285290
if (name == null)
286291
{
@@ -305,7 +310,11 @@ private static IHttpClientBuilder AddGrpcClientCore<TClient>(this IServiceCollec
305310
/// <summary>
306311
/// 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
307312
/// </summary>
308-
private static IHttpClientBuilder AddGrpcHttpClient<TClient>(this IServiceCollection services, string name)
313+
private static IHttpClientBuilder AddGrpcHttpClient<
314+
#if NET5_0_OR_GREATER
315+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
316+
#endif
317+
TClient>(this IServiceCollection services, string name)
309318
where TClient : class
310319
{
311320
if (services == null)

src/Grpc.Net.ClientFactory/Internal/DefaultClientActivator.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@
1616

1717
#endregion
1818

19+
using System.Diagnostics.CodeAnalysis;
1920
using Grpc.Core;
2021
using Microsoft.Extensions.DependencyInjection;
2122

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

src/Grpc.Net.ClientFactory/Internal/DefaultGrpcClientFactory.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#endregion
1818

19+
using System.Diagnostics.CodeAnalysis;
1920
using Grpc.Core;
2021
using Grpc.Core.Interceptors;
2122
using Microsoft.Extensions.DependencyInjection;
@@ -38,7 +39,11 @@ public DefaultGrpcClientFactory(IServiceProvider serviceProvider,
3839
_grpcClientFactoryOptionsMonitor = grpcClientFactoryOptionsMonitor;
3940
}
4041

41-
public override TClient CreateClient<TClient>(string name) where TClient : class
42+
public override TClient CreateClient<
43+
#if NET5_0_OR_GREATER
44+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
45+
#endif
46+
TClient>(string name) where TClient : class
4247
{
4348
var defaultClientActivator = _serviceProvider.GetService<DefaultClientActivator<TClient>>();
4449
if (defaultClientActivator == null)

0 commit comments

Comments
 (0)