use HttpClientFactory and Polly

pull/1170/head
Mon 6 years ago
parent 5479701d89
commit 5499c87db0

@ -1,4 +1,4 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.168
MinimumVisualStudioVersion = 10.0.40219.1

@ -8,15 +8,20 @@ namespace Volo.Abp.AspNetCore.TestBase.DynamicProxying
public class AspNetCoreTestDynamicProxyHttpClientFactory : IDynamicProxyHttpClientFactory, ITransientDependency
{
private readonly ITestServerAccessor _testServerAccessor;
private readonly IHttpClientFactory _httpClientFactory;
public AspNetCoreTestDynamicProxyHttpClientFactory(ITestServerAccessor testServerAccessor)
{
public AspNetCoreTestDynamicProxyHttpClientFactory(ITestServerAccessor testServerAccessor, IHttpClientFactory httpClientFactory) {
_testServerAccessor = testServerAccessor;
_httpClientFactory = httpClientFactory;
}
public HttpClient Create()
{
return _testServerAccessor.Server.CreateClient();
}
public HttpClient Create(string name) {
return Create();
}
}
}

@ -28,7 +28,7 @@ namespace Volo.Abp.Http.Client.IdentityModel
var accessToken = await GetAccessTokenFromHttpContextOrNullAsync();
if (accessToken != null)
{
context.Client.SetBearerToken(accessToken);
context.Request.SetBearerToken(accessToken);
return;
}
}

@ -3,6 +3,7 @@ using System.Linq;
using System.Reflection;
using Castle.DynamicProxy;
using JetBrains.Annotations;
using Polly;
using Volo.Abp;
using Volo.Abp.Castle.DynamicProxy;
using Volo.Abp.Http.Client;
@ -106,6 +107,12 @@ namespace Microsoft.Extensions.DependencyInjection
options.HttpClientProxies[type] = new DynamicHttpClientProxyConfig(type, remoteServiceConfigurationName);
});
//use IHttpClientFactory and polly
services.AddHttpClient(remoteServiceConfigurationName)
.AddTransientHttpErrorPolicy(builder =>
// retry 3 times
builder.WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(Math.Pow(2, i))));
var interceptorType = typeof(DynamicHttpProxyInterceptor<>).MakeGenericType(type);
services.AddTransient(interceptorType);

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\common.props" />
@ -15,6 +15,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="2.2.0" />
</ItemGroup>
<ItemGroup>

@ -5,9 +5,21 @@ namespace Volo.Abp.Http.Client.DynamicProxying
{
public class DefaultDynamicProxyHttpClientFactory : IDynamicProxyHttpClientFactory, ITransientDependency
{
private readonly IHttpClientFactory _httpClientFactory;
public DefaultDynamicProxyHttpClientFactory(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public HttpClient Create()
{
return new HttpClient();
return _httpClientFactory.CreateClient();
}
public HttpClient Create(string name)
{
return _httpClientFactory.CreateClient(name);
}
}
}

@ -127,12 +127,12 @@ namespace Volo.Abp.Http.Client.DynamicProxying
}
private async Task<string> MakeRequestAsync(IAbpMethodInvocation invocation)
{
using (var client = HttpClientFactory.Create())
{
var clientConfig = ClientOptions.HttpClientProxies.GetOrDefault(typeof(TService)) ?? throw new AbpException($"Could not get DynamicHttpClientProxyConfig for {typeof(TService).FullName}.");
var remoteServiceConfig = RemoteServiceOptions.RemoteServices.GetConfigurationOrDefault(clientConfig.RemoteServiceName);
var client = HttpClientFactory.Create(clientConfig.RemoteServiceName);
var action = await ApiDescriptionFinder.FindActionAsync(remoteServiceConfig.BaseUrl, typeof(TService), invocation.Method);
var apiVersion = GetApiVersionInfo(action);
var url = remoteServiceConfig.BaseUrl + UrlBuilder.GenerateUrlWithParameters(action, invocation.ArgumentsDictionary, apiVersion);
@ -162,7 +162,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
return await response.Content.ReadAsStringAsync();
}
}
private ApiVersionInfo GetApiVersionInfo(ActionApiDescriptionModel action)
{

@ -5,5 +5,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
public interface IDynamicProxyHttpClientFactory
{
HttpClient Create();
HttpClient Create(string name);
}
}
Loading…
Cancel
Save