Browse Source
Add `ProxyHttpClientPreSendActions` to `AbpHttpClientOptions`.
pull/21484/head
maliming
2 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with
50 additions and
1 deletions
-
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientOptions.cs
-
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyBase.cs
-
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpClientTestModule.cs
-
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/IRegularTestController.cs
-
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs
-
framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.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,17 @@ public class AbpHttpClientOptions |
|
|
|
{ |
|
|
|
public Dictionary<Type, HttpClientProxyConfig> HttpClientProxies { get; set; } |
|
|
|
|
|
|
|
public Dictionary<string, List<Action<HttpClientProxyConfig, ClientProxyRequestContext, HttpClient>>> ProxyHttpClientPreSendActions { get; } |
|
|
|
|
|
|
|
public AbpHttpClientOptions() |
|
|
|
{ |
|
|
|
HttpClientProxies = new Dictionary<Type, HttpClientProxyConfig>(); |
|
|
|
ProxyHttpClientPreSendActions = new Dictionary<string, List<Action<HttpClientProxyConfig, ClientProxyRequestContext, HttpClient>>>(); |
|
|
|
} |
|
|
|
|
|
|
|
public AbpHttpClientOptions AddPreSendAction(string remoteServiceName, Action<HttpClientProxyConfig, ClientProxyRequestContext, HttpClient> action) |
|
|
|
{ |
|
|
|
ProxyHttpClientPreSendActions.GetOrAdd(remoteServiceName, () => new List<Action<HttpClientProxyConfig, ClientProxyRequestContext, HttpClient>>()).Add(action); |
|
|
|
return this; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -146,6 +146,11 @@ public class ClientProxyBase<TService> : ITransientDependency |
|
|
|
HttpResponseMessage response; |
|
|
|
try |
|
|
|
{ |
|
|
|
foreach (var preSendAction in ClientOptions.Value.ProxyHttpClientPreSendActions.Where(x => x.Key == clientConfig.RemoteServiceName).SelectMany(x => x.Value)) |
|
|
|
{ |
|
|
|
preSendAction(clientConfig, requestContext, client); |
|
|
|
} |
|
|
|
|
|
|
|
response = await client.SendAsync( |
|
|
|
requestMessage, |
|
|
|
HttpCompletionOption.ResponseHeadersRead /*this will buffer only the headers, the content will be used as a stream*/, |
|
|
|
|
|
|
|
@ -1,8 +1,11 @@ |
|
|
|
using System.Collections.Generic; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Net.Http; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Volo.Abp.AspNetCore.Mvc; |
|
|
|
using Volo.Abp.Http.Client; |
|
|
|
using Volo.Abp.Http.Client.ClientProxying; |
|
|
|
using Volo.Abp.Http.Client.Proxying; |
|
|
|
using Volo.Abp.Http.DynamicProxying; |
|
|
|
using Volo.Abp.Http.Localization; |
|
|
|
using Volo.Abp.Localization; |
|
|
|
@ -60,5 +63,16 @@ public class AbpHttpClientTestModule : AbpModule |
|
|
|
options.FormDataConverts.Add(typeof(List<GetParamsNameValue>), typeof(TestObjectToFormData)); |
|
|
|
options.PathConverts.Add(typeof(int), typeof(TestObjectToPath)); |
|
|
|
}); |
|
|
|
|
|
|
|
Configure<AbpHttpClientOptions>(options => |
|
|
|
{ |
|
|
|
options.AddPreSendAction("Default", (_, requestContext, httpclient) => |
|
|
|
{ |
|
|
|
if (requestContext.Action.Name.Equals("TimeOutRequestAsync")) |
|
|
|
{ |
|
|
|
httpclient.Timeout = TimeSpan.FromMilliseconds(1); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -43,4 +43,7 @@ public interface IRegularTestController |
|
|
|
Task<int> DeleteByIdAsync(int id); |
|
|
|
|
|
|
|
Task<string> AbortRequestAsync(CancellationToken cancellationToken = default); |
|
|
|
|
|
|
|
Task<string> TimeOutRequestAsync(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@ -152,6 +152,14 @@ public class RegularTestController : AbpController, IRegularTestController |
|
|
|
await Task.Delay(100, cancellationToken); |
|
|
|
return "AbortRequestAsync"; |
|
|
|
} |
|
|
|
|
|
|
|
[HttpGet] |
|
|
|
[Route("timeout-request")] |
|
|
|
public async Task<string> TimeOutRequestAsync() |
|
|
|
{ |
|
|
|
await Task.Delay(100); |
|
|
|
return "TimeOutRequestAsync"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public class Car |
|
|
|
|
|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using System; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
@ -187,4 +188,11 @@ public class RegularTestControllerClientProxy_Tests : AbpHttpClientTestBase |
|
|
|
var exception = await Assert.ThrowsAsync<AbpRemoteCallException>(async () => await _controller.AbortRequestAsync(cts.Token)); |
|
|
|
exception.InnerException.InnerException.InnerException.Message.ShouldBe("The client aborted the request."); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task TimeOutRequestAsync() |
|
|
|
{ |
|
|
|
var exception = await Assert.ThrowsAsync<HttpRequestException>(async () => await _controller.TimeOutRequestAsync()); |
|
|
|
exception.InnerException.InnerException.Message.ShouldBe("The client aborted the request."); |
|
|
|
} |
|
|
|
} |
|
|
|
|