diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientExecuteHttpActionOptions.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientExecuteHttpActionOptions.cs deleted file mode 100644 index 65e1613c7a..0000000000 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientExecuteHttpActionOptions.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Net.Http; -using System.Reflection; -using System.Text; -using Volo.Abp.Http.Modeling; - -namespace Volo.Abp.Http.Client; -public class AbpHttpClientExecuteHttpActionOptions -{ - public Action? ExecuteHttpAction{ get; set; } -} diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientOptions.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientOptions.cs index 6743ab5174..0311423504 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientOptions.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientOptions.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Net.Http; +using Volo.Abp.Http.Client.ClientProxying; using Volo.Abp.Http.Client.Proxying; namespace Volo.Abp.Http.Client; @@ -8,8 +10,11 @@ public class AbpHttpClientOptions { public Dictionary HttpClientProxies { get; set; } + public Dictionary> ProxyHttpClientPreSendActions { get; } + public AbpHttpClientOptions() { HttpClientProxies = new Dictionary(); + ProxyHttpClientPreSendActions = new Dictionary>(); } } diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyBase.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyBase.cs index a1f847f10f..9036b552f9 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyBase.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyBase.cs @@ -7,7 +7,6 @@ using System.Net.Http.Headers; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; -using Castle.DynamicProxy; using Microsoft.Extensions.Options; using Microsoft.Extensions.Primitives; using Volo.Abp.Content; @@ -44,8 +43,6 @@ public class ClientProxyBase : ITransientDependency protected ICurrentApiVersionInfo CurrentApiVersionInfo => LazyServiceProvider.LazyGetRequiredService(); protected ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService(); - protected IOptions ExecuteHttpActionOptions => LazyServiceProvider.LazyGetRequiredService>(); - protected virtual async Task RequestAsync(string methodName, ClientProxyRequestTypeValue? arguments = null) { await RequestAsync(BuildHttpProxyClientProxyContext(methodName, arguments)); @@ -148,9 +145,11 @@ public class ClientProxyBase : ITransientDependency HttpResponseMessage response; try - { - //Allows users to customize the timeout for remote methods of specific requests - ExecuteHttpActionOptions.Value.ExecuteHttpAction?.Invoke(requestContext.Action, client); + { + foreach (var preSendAction in ClientOptions.Value.ProxyHttpClientPreSendActions.Where(x => x.Key == clientConfig.RemoteServiceName).Select(x => x.Value)) + { + preSendAction(clientConfig, requestContext, client); + } response = await client.SendAsync( requestMessage, diff --git a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpClientTestModule.cs b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpClientTestModule.cs index 0da4d482dc..8af7a5bbbe 100644 --- a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpClientTestModule.cs +++ b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpClientTestModule.cs @@ -62,16 +62,15 @@ public class AbpHttpClientTestModule : AbpModule options.PathConverts.Add(typeof(int), typeof(TestObjectToPath)); }); - Configure(options => + Configure(options => { - options.ExecuteHttpAction = (method, httpclient) => + options.ProxyHttpClientPreSendActions.Add("Default", (_, requestContext, httpclient) => { - if (method.Name.Equals("TimeOutRequestAsync")) - { - httpclient.Timeout = TimeSpan.FromMilliseconds(2000); + if (requestContext.Action.Name.Equals("TimeOutRequestAsync")) + { + httpclient.Timeout = TimeSpan.FromMilliseconds(1); } - }; + }); }); - } } diff --git a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs index c353f73e93..46b355090b 100644 --- a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs +++ b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs @@ -157,8 +157,8 @@ public class RegularTestController : AbpController, IRegularTestController [Route("timeout-request")] public async Task TimeOutRequestAsync() { - await Task.Delay(5000); - return "timeout-request"; + await Task.Delay(100); + return "TimeOutRequestAsync"; } } diff --git a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs index 4f095d468d..01ec97d734 100644 --- a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs +++ b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs @@ -1,4 +1,5 @@ using System; +using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; @@ -191,12 +192,7 @@ public class RegularTestControllerClientProxy_Tests : AbpHttpClientTestBase [Fact] public async Task TimeOutRequestAsync() { - //This cannot be executed successfully because the unit test does not use SocketsHttpHandler for request processing, - //so the desired results cannot be obtained locally. At the same time, - //I can achieve the expected results by using other services locally. - - //var exception = await Assert.ThrowsAsync(async () => await _controller.TimeOutRequestAsync()); - //exception.InnerException.InnerException.InnerException.Message.ShouldBe("The client aborted the request."); - await Task.CompletedTask; + var exception = await Assert.ThrowsAsync(async () => await _controller.TimeOutRequestAsync()); + exception.InnerException.InnerException.Message.ShouldBe("The client aborted the request."); } }