Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid calling AddHttpClient & HttpErrorPolicy multiple times. #2201

Merged
merged 1 commit into from
Dec 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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