diff --git a/src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs b/src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs index 74da27aaf..f6097e1e8 100644 --- a/src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs +++ b/src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs @@ -35,9 +35,10 @@ internal void ConfigureClient(IServiceCollection services, IHttpClientBuilder bu builder.AddCallCredentials(async (_, metadata, serviceProvider) => { - var tokenProvider = serviceProvider.GetService(); - if (tokenProvider is not null) + var tokenProviderFactory = serviceProvider.GetService>(); + if (tokenProviderFactory is not null) { + var tokenProvider = tokenProviderFactory.GetProvider(serviceProvider); var token = await tokenProvider.GetTokenAsync(); if (!string.IsNullOrEmpty(token)) { @@ -45,9 +46,11 @@ internal void ConfigureClient(IServiceCollection services, IHttpClientBuilder bu } } - var metadataProviders = serviceProvider.GetServices(); - foreach (var metadataProvider in metadataProviders) + + var metadataProviderFactory = serviceProvider.GetService>(); + if (metadataProviderFactory is not null) { + var metadataProvider = metadataProviderFactory.GetProvider(serviceProvider); var newMetadata = await metadataProvider.GetMetadataAsync(); if (newMetadata?.Count > 0) { @@ -76,7 +79,8 @@ public GrpcClientModuleOptions AddMetadataProvider() { configureServicesActions.Add(services => { - services.AddTransient(); + services.AddTransient, GrpcMetadataProviderFactory>(); + services.TryAddTransient(); }); return this; } @@ -86,6 +90,7 @@ public GrpcClientModuleOptions AddTokenAuth() { configureServicesActions.Add(services => { + services.AddTransient, GrpcTokenProviderFactory>(); services.TryAddTransient(); }); return this; diff --git a/src/Sitko.Core.Grpc.Client/IGrpcMetadataProviderFactory.cs b/src/Sitko.Core.Grpc.Client/IGrpcMetadataProviderFactory.cs new file mode 100644 index 000000000..ca11839a8 --- /dev/null +++ b/src/Sitko.Core.Grpc.Client/IGrpcMetadataProviderFactory.cs @@ -0,0 +1,16 @@ +using Grpc.Core; +using Microsoft.Extensions.DependencyInjection; + +namespace Sitko.Core.Grpc.Client; + +internal interface IGrpcMetadataProviderFactory where TClient : ClientBase +{ + public IGrpcMetadataProvider GetProvider(IServiceProvider serviceProvider); +} + +internal class GrpcMetadataProviderFactory : IGrpcMetadataProviderFactory where TClient : ClientBase + where TMetadataProvider : class, IGrpcMetadataProvider + +{ + public IGrpcMetadataProvider GetProvider(IServiceProvider serviceProvider) => serviceProvider.GetService()!; +} diff --git a/src/Sitko.Core.Grpc.Client/IGrpcTokenProviderFactory.cs b/src/Sitko.Core.Grpc.Client/IGrpcTokenProviderFactory.cs new file mode 100644 index 000000000..d4d8bf508 --- /dev/null +++ b/src/Sitko.Core.Grpc.Client/IGrpcTokenProviderFactory.cs @@ -0,0 +1,16 @@ +using Grpc.Core; +using Microsoft.Extensions.DependencyInjection; + +namespace Sitko.Core.Grpc.Client; + +public interface IGrpcTokenProviderFactory where TClient : ClientBase +{ + public IGrpcTokenProvider GetProvider(IServiceProvider serviceProvider); +} + +internal class GrpcTokenProviderFactory : IGrpcTokenProviderFactory where TClient : ClientBase + where TTokenProvider : class, IGrpcTokenProvider + +{ + public IGrpcTokenProvider GetProvider(IServiceProvider serviceProvider) => serviceProvider.GetService()!; +}