From 69ef0e75c605a2e208860685abb28f60d63cc033 Mon Sep 17 00:00:00 2001 From: nadia Date: Tue, 21 May 2024 15:28:40 +0500 Subject: [PATCH] fix: get generic grpc token\metadata provider --- src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs | 4 ++-- src/Sitko.Core.Grpc.Client/IGrpcMetadataProviderFactory.cs | 7 +++++-- src/Sitko.Core.Grpc.Client/IGrpcTokenProviderFactory.cs | 7 +++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs b/src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs index f6097e1e8..5116b4b76 100644 --- a/src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs +++ b/src/Sitko.Core.Grpc.Client/GrpcClientModuleOptions.cs @@ -80,7 +80,7 @@ public GrpcClientModuleOptions AddMetadataProvider() configureServicesActions.Add(services => { services.AddTransient, GrpcMetadataProviderFactory>(); - services.TryAddTransient(); + services.AddTransient(); }); return this; } @@ -91,7 +91,7 @@ public GrpcClientModuleOptions AddTokenAuth() configureServicesActions.Add(services => { services.AddTransient, GrpcTokenProviderFactory>(); - services.TryAddTransient(); + services.AddTransient(); }); return this; } diff --git a/src/Sitko.Core.Grpc.Client/IGrpcMetadataProviderFactory.cs b/src/Sitko.Core.Grpc.Client/IGrpcMetadataProviderFactory.cs index ca11839a8..281781fb6 100644 --- a/src/Sitko.Core.Grpc.Client/IGrpcMetadataProviderFactory.cs +++ b/src/Sitko.Core.Grpc.Client/IGrpcMetadataProviderFactory.cs @@ -8,9 +8,12 @@ internal interface IGrpcMetadataProviderFactory where TClient : ClientB public IGrpcMetadataProvider GetProvider(IServiceProvider serviceProvider); } -internal class GrpcMetadataProviderFactory : IGrpcMetadataProviderFactory where TClient : ClientBase +internal class GrpcMetadataProviderFactory : IGrpcMetadataProviderFactory + where TClient : ClientBase where TMetadataProvider : class, IGrpcMetadataProvider { - public IGrpcMetadataProvider GetProvider(IServiceProvider serviceProvider) => serviceProvider.GetService()!; + public IGrpcMetadataProvider GetProvider(IServiceProvider serviceProvider) => + serviceProvider.GetServices().OfType().FirstOrDefault() ?? + throw new InvalidDataException($"Service {typeof(TMetadataProvider)} is not register"); } diff --git a/src/Sitko.Core.Grpc.Client/IGrpcTokenProviderFactory.cs b/src/Sitko.Core.Grpc.Client/IGrpcTokenProviderFactory.cs index d4d8bf508..a5842c0b6 100644 --- a/src/Sitko.Core.Grpc.Client/IGrpcTokenProviderFactory.cs +++ b/src/Sitko.Core.Grpc.Client/IGrpcTokenProviderFactory.cs @@ -8,9 +8,12 @@ public interface IGrpcTokenProviderFactory where TClient : ClientBase : IGrpcTokenProviderFactory where TClient : ClientBase +internal class GrpcTokenProviderFactory : IGrpcTokenProviderFactory + where TClient : ClientBase where TTokenProvider : class, IGrpcTokenProvider { - public IGrpcTokenProvider GetProvider(IServiceProvider serviceProvider) => serviceProvider.GetService()!; + public IGrpcTokenProvider GetProvider(IServiceProvider serviceProvider) => + serviceProvider.GetServices().OfType().FirstOrDefault() ?? + throw new InvalidDataException($"Service {typeof(TTokenProvider)} is not register"); }