diff --git a/framework/Volo.Abp.sln b/framework/Volo.Abp.sln index be77f0ab24..3d0beab360 100644 --- a/framework/Volo.Abp.sln +++ b/framework/Volo.Abp.sln @@ -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 diff --git a/framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/DynamicProxying/AspNetCoreTestDynamicProxyHttpClientFactory.cs b/framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/DynamicProxying/AspNetCoreTestDynamicProxyHttpClientFactory.cs index e0253f8f8b..e8463e16cc 100644 --- a/framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/DynamicProxying/AspNetCoreTestDynamicProxyHttpClientFactory.cs +++ b/framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/DynamicProxying/AspNetCoreTestDynamicProxyHttpClientFactory.cs @@ -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(); + } } } diff --git a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs b/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs index 91d1c4cb9b..eab45ca96b 100644 --- a/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs +++ b/framework/src/Volo.Abp.Http.Client.IdentityModel/Volo/Abp/Http/Client/IdentityModel/IdentityModelRemoteServiceHttpClientAuthenticator.cs @@ -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; } } 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 7689e94494..21212ee16a 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 @@ -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; @@ -105,6 +106,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); diff --git a/framework/src/Volo.Abp.Http.Client/Volo.Abp.Http.Client.csproj b/framework/src/Volo.Abp.Http.Client/Volo.Abp.Http.Client.csproj index f6afd53f1d..1c7f727e18 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo.Abp.Http.Client.csproj +++ b/framework/src/Volo.Abp.Http.Client/Volo.Abp.Http.Client.csproj @@ -1,4 +1,4 @@ - + @@ -15,6 +15,7 @@ + diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DefaultDynamicProxyHttpClientFactory.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DefaultDynamicProxyHttpClientFactory.cs index 6c827f5237..e4aad53a11 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DefaultDynamicProxyHttpClientFactory.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DefaultDynamicProxyHttpClientFactory.cs @@ -1,13 +1,25 @@ using System.Net.Http; using Volo.Abp.DependencyInjection; -namespace Volo.Abp.Http.Client.DynamicProxying +namespace Volo.Abp.Http.Client.DynamicProxying { - public class DefaultDynamicProxyHttpClientFactory : IDynamicProxyHttpClientFactory, ITransientDependency + public class DefaultDynamicProxyHttpClientFactory : IDynamicProxyHttpClientFactory, ITransientDependency { - public HttpClient Create() + private readonly IHttpClientFactory _httpClientFactory; + + public DefaultDynamicProxyHttpClientFactory(IHttpClientFactory httpClientFactory) + { + _httpClientFactory = httpClientFactory; + } + + public HttpClient Create() + { + return _httpClientFactory.CreateClient(); + } + + public HttpClient Create(string name) { - return new HttpClient(); + return _httpClientFactory.CreateClient(name); } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs index 1a79d4308a..5b200daf85 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/DynamicHttpProxyInterceptor.cs @@ -128,41 +128,41 @@ namespace Volo.Abp.Http.Client.DynamicProxying private async Task 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 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 action = await ApiDescriptionFinder.FindActionAsync(remoteServiceConfig.BaseUrl, typeof(TService), invocation.Method); - var apiVersion = GetApiVersionInfo(action); - var url = remoteServiceConfig.BaseUrl + UrlBuilder.GenerateUrlWithParameters(action, invocation.ArgumentsDictionary, apiVersion); + var client = HttpClientFactory.Create(clientConfig.RemoteServiceName); - var requestMessage = new HttpRequestMessage(action.GetHttpMethod(), url) - { - Content = RequestPayloadBuilder.BuildContent(action, invocation.ArgumentsDictionary, JsonSerializer, apiVersion) - }; - - AddHeaders(invocation, action, requestMessage, apiVersion); - - await ClientAuthenticator.Authenticate( - new RemoteServiceHttpClientAuthenticateContext( - client, - requestMessage, - remoteServiceConfig, - 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); - var response = await client.SendAsync(requestMessage, GetCancellationToken()); + var requestMessage = new HttpRequestMessage(action.GetHttpMethod(), url) + { + Content = RequestPayloadBuilder.BuildContent(action, invocation.ArgumentsDictionary, JsonSerializer, apiVersion) + }; - if (!response.IsSuccessStatusCode) - { - await ThrowExceptionForResponseAsync(response); - } + AddHeaders(invocation, action, requestMessage, apiVersion); - return await response.Content.ReadAsStringAsync(); + await ClientAuthenticator.Authenticate( + new RemoteServiceHttpClientAuthenticateContext( + client, + requestMessage, + remoteServiceConfig, + clientConfig.RemoteServiceName + ) + ); + + var response = await client.SendAsync(requestMessage, GetCancellationToken()); + + if (!response.IsSuccessStatusCode) + { + await ThrowExceptionForResponseAsync(response); } - } + + return await response.Content.ReadAsStringAsync(); + } + private ApiVersionInfo GetApiVersionInfo(ActionApiDescriptionModel action) { diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/IDynamicProxyHttpClientFactory.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/IDynamicProxyHttpClientFactory.cs index aaf821d0f9..1cc90672a1 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/IDynamicProxyHttpClientFactory.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/IDynamicProxyHttpClientFactory.cs @@ -5,5 +5,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying public interface IDynamicProxyHttpClientFactory { HttpClient Create(); + + HttpClient Create(string name); } } \ No newline at end of file