Skip to content

Commit

Permalink
Avoid calling AddHttpClient & HttpErrorPolicy multiple times.
Browse files Browse the repository at this point in the history
Resolve #2009
  • Loading branch information
maliming committed Nov 18, 2019
1 parent 10cb677 commit f9af889
Showing 1 changed file with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -53,9 +55,8 @@ public static IServiceCollection AddHttpClientProxies(
services.AddHttpClientProxy(
serviceType,
remoteServiceConfigurationName,
asDefaultServices,
configureHttpClientBuilder
);
asDefaultServices
);
}

return services;
Expand All @@ -82,14 +83,46 @@ public static IServiceCollection AddHttpClientProxy<T>(
bool asDefaultService = true,
Action<IHttpClientBuilder> configureHttpClientBuilder = null)
{
AddHttpClientFactoryAndPolicy(services, remoteServiceConfigurationName, configureHttpClientBuilder);

return services.AddHttpClientProxy(
typeof(T),
remoteServiceConfigurationName,
asDefaultService,
configureHttpClientBuilder
asDefaultService
);
}

/// <summary>
/// Use IHttpClientFactory and polly
/// </summary>
/// <param name="services">Service collection</param>
/// <param name="remoteServiceConfigurationName">
/// The name of the remote service configuration to be used by the HTTP Client proxies.
/// See <see cref="AbpRemoteServiceOptions"/>.
/// </param>
/// <param name="configureHttpClientBuilder">
/// A delegate that is used to configure an <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" />.
/// </param>
public static IServiceCollection AddHttpClientFactoryAndPolicy(
[NotNull] this IServiceCollection services,
[NotNull] string remoteServiceConfigurationName = RemoteServiceConfigurationDictionary.DefaultName,
Action<IHttpClientBuilder> 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;
}

/// <summary>
/// Registers HTTP Client Proxy for given service <paramref name="type"/>.
/// </summary>
Expand All @@ -102,15 +135,11 @@ public static IServiceCollection AddHttpClientProxy<T>(
/// <param name="asDefaultService">
/// True, to register the HTTP client proxy as the default implementation for the service <paramref name="type"/>.
/// </param>
/// <param name="configureHttpClientBuilder">
/// A delegate that is used to configure an <see cref="T:Microsoft.Extensions.DependencyInjection.IHttpClientBuilder" />.
/// </param>
public static IServiceCollection AddHttpClientProxy(
[NotNull] this IServiceCollection services,
[NotNull] Type type,
[NotNull] string remoteServiceConfigurationName = RemoteServiceConfigurationDictionary.DefaultName,
bool asDefaultService = true,
Action<IHttpClientBuilder> configureHttpClientBuilder = null)
bool asDefaultService = true)
{
Check.NotNull(services, nameof(services));
Check.NotNull(type, nameof(type));
Expand All @@ -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);
Expand Down

0 comments on commit f9af889

Please sign in to comment.