From f9af88974374b016e05f830d0b8ffcd5dc5eea3d Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 18 Nov 2019 15:28:26 +0800 Subject: [PATCH] Avoid calling AddHttpClient & HttpErrorPolicy multiple times. Resolve #2009 --- ...lectionDynamicHttpClientProxyExtensions.cs | 62 ++++++++++++------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/framework/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs b/framework/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs index 1ccf7a2af50..54387d28715 100644 --- a/framework/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs +++ b/framework/src/Volo.Abp.Http.Client/Microsoft/Extensions/DependencyInjection/ServiceCollectionDynamicHttpClientProxyExtensions.cs @@ -41,6 +41,8 @@ public static IServiceCollection AddHttpClientProxies( { Check.NotNull(services, nameof(assembly)); + AddHttpClientFactoryAndPolicy(services, remoteServiceConfigurationName, configureHttpClientBuilder); + //TODO: Make a configuration option and add remoteServiceName inside it! //TODO: Add option to change type filter @@ -53,9 +55,8 @@ public static IServiceCollection AddHttpClientProxies( services.AddHttpClientProxy( serviceType, remoteServiceConfigurationName, - asDefaultServices, - configureHttpClientBuilder - ); + asDefaultServices + ); } return services; @@ -82,14 +83,46 @@ public static IServiceCollection AddHttpClientProxy( bool asDefaultService = true, Action configureHttpClientBuilder = null) { + AddHttpClientFactoryAndPolicy(services, remoteServiceConfigurationName, configureHttpClientBuilder); + return services.AddHttpClientProxy( typeof(T), remoteServiceConfigurationName, - asDefaultService, - configureHttpClientBuilder + asDefaultService ); } + /// + /// Use IHttpClientFactory and polly + /// + /// Service collection + /// + /// The name of the remote service configuration to be used by the HTTP Client proxies. + /// See . + /// + /// + /// A delegate that is used to configure an . + /// + public static IServiceCollection AddHttpClientFactoryAndPolicy( + [NotNull] this IServiceCollection services, + [NotNull] string remoteServiceConfigurationName = RemoteServiceConfigurationDictionary.DefaultName, + Action configureHttpClientBuilder = null) + { + var httpClientBuilder = services.AddHttpClient(remoteServiceConfigurationName); + if (configureHttpClientBuilder == null) + { + httpClientBuilder.AddTransientHttpErrorPolicy(builder => + // retry 3 times + builder.WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(Math.Pow(2, i)))); + } + else + { + configureHttpClientBuilder.Invoke(httpClientBuilder); + } + + return services; + } + /// /// Registers HTTP Client Proxy for given service . /// @@ -102,15 +135,11 @@ public static IServiceCollection AddHttpClientProxy( /// /// True, to register the HTTP client proxy as the default implementation for the service . /// - /// - /// A delegate that is used to configure an . - /// public static IServiceCollection AddHttpClientProxy( [NotNull] this IServiceCollection services, [NotNull] Type type, [NotNull] string remoteServiceConfigurationName = RemoteServiceConfigurationDictionary.DefaultName, - bool asDefaultService = true, - Action configureHttpClientBuilder = null) + bool asDefaultService = true) { Check.NotNull(services, nameof(services)); Check.NotNull(type, nameof(type)); @@ -120,19 +149,6 @@ public static IServiceCollection AddHttpClientProxy( { options.HttpClientProxies[type] = new DynamicHttpClientProxyConfig(type, remoteServiceConfigurationName); }); - - //use IHttpClientFactory and polly - var httpClientBuilder = services.AddHttpClient(remoteServiceConfigurationName); - if (configureHttpClientBuilder == null) - { - httpClientBuilder.AddTransientHttpErrorPolicy(builder => - // retry 3 times - builder.WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(Math.Pow(2, i)))); - } - else - { - configureHttpClientBuilder.Invoke(httpClientBuilder); - } var interceptorType = typeof(DynamicHttpProxyInterceptor<>).MakeGenericType(type); services.AddTransient(interceptorType);