Skip to content

Commit 52f7ea8

Browse files
committed
HttpClientContext增加OptionsName属性
1 parent cc42f19 commit 52f7ea8

File tree

7 files changed

+47
-22
lines changed

7 files changed

+47
-22
lines changed

WebApiClientCore.Extensions.NewtonsoftJson/Attributes/JsonNetContentAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public string CharSet
3838
/// <returns></returns>
3939
protected override Task SetHttpContentAsync(ApiParameterContext context)
4040
{
41-
var name = HttpApi.GetName(context.ApiAction.InterfaceType);
41+
var name = context.HttpContext.OptionsName;
4242
var options = context.HttpContext.ServiceProvider.GetService<IOptionsMonitor<JsonNetSerializerOptions>>().Get(name);
4343
var json = context.ParameterValue == null
4444
? string.Empty

WebApiClientCore.Extensions.NewtonsoftJson/Attributes/JsonNetReturnAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public override async Task SetResultAsync(ApiResponseContext context)
4545
{
4646
var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
4747
var resultType = context.ApiAction.Return.DataType.Type;
48-
var name = HttpApi.GetName(context.ApiAction.InterfaceType);
48+
49+
var name = context.HttpContext.OptionsName;
4950
var options = context.HttpContext.ServiceProvider.GetService<IOptionsMonitor<JsonNetSerializerOptions>>().Get(name);
5051

5152
context.Result = JsonConvert.DeserializeObject(json, resultType, options.JsonDeserializeOptions);

WebApiClientCore.Test/TestRequestContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ private static HttpContext GetHttpContext()
3232

3333
var requestServices = services.BuildServiceProvider();
3434
var options = new HttpApiOptions() { HttpHost = new Uri("http://www.webapi.com/") };
35-
return new HttpContext(new HttpClient(), requestServices, options);
35+
36+
var httpClientContext = new HttpClientContext(new HttpClient(), requestServices, options);
37+
return new HttpContext(httpClientContext);
3638
}
3739
}
3840
}

WebApiClientCore/HttpApi.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace WebApiClientCore
88
/// 提供创建THttpApi的代理实例
99
/// </summary>
1010
public static class HttpApi
11-
{
11+
{
1212
/// <summary>
13-
/// 获取接口的命名
14-
/// 该命名用于接口对应的Options名称
13+
/// 获取接口的别名
14+
/// 该别名可用于接口对应的OptionsName
1515
/// </summary>
1616
/// <param name="httpApiType">接口类型</param>
1717
/// <returns></returns>

WebApiClientCore/HttpClientContext.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class HttpClientContext
2323
/// </summary>
2424
public IServiceProvider ServiceProvider { get; }
2525

26+
/// <summary>
27+
/// 获取选项名称
28+
/// </summary>
29+
public string OptionsName { get; }
30+
2631
/// <summary>
2732
/// HttpClient上下文
2833
/// </summary>
@@ -31,10 +36,37 @@ public class HttpClientContext
3136
/// <param name="httpApiOptions">Api配置选项</param>
3237
/// <exception cref="ArgumentNullException"></exception>
3338
public HttpClientContext(HttpClient httpClient, IServiceProvider serviceProvider, HttpApiOptions httpApiOptions)
39+
: this(httpClient, serviceProvider, httpApiOptions, string.Empty)
40+
{
41+
}
42+
43+
/// <summary>
44+
/// HttpClient上下文
45+
/// </summary>
46+
/// <param name="httpClient">httpClient实例</param>
47+
/// <param name="serviceProvider">服务提供者</param>
48+
/// <param name="httpApiOptions">Api配置选项</param>
49+
/// <param name="optionsName">选项名称</param>
50+
/// <exception cref="ArgumentNullException"></exception>
51+
public HttpClientContext(HttpClient httpClient, IServiceProvider serviceProvider, HttpApiOptions httpApiOptions, string optionsName)
3452
{
3553
this.HttpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
3654
this.ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
3755
this.HttpApiOptions = httpApiOptions ?? throw new ArgumentNullException(nameof(httpApiOptions));
56+
this.OptionsName = optionsName ?? string.Empty;
57+
}
58+
59+
/// <summary>
60+
/// 转换为字符串
61+
/// </summary>
62+
/// <returns></returns>
63+
public override string ToString()
64+
{
65+
if (string.IsNullOrEmpty(this.OptionsName))
66+
{
67+
return base.ToString();
68+
}
69+
return this.OptionsName;
3870
}
3971
}
4072
}

WebApiClientCore/HttpContext.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,10 @@ public class HttpContext : HttpClientContext, IDisposable
3131
/// <param name="context">服务上下文</param>
3232
/// <exception cref="ArgumentNullException"></exception>
3333
public HttpContext(HttpClientContext context)
34-
: this(context.HttpClient, context.ServiceProvider, context.HttpApiOptions)
34+
: base(context.HttpClient, context.ServiceProvider, context.HttpApiOptions, context.OptionsName)
3535
{
36-
}
37-
38-
/// <summary>
39-
/// http上下文
40-
/// </summary>
41-
/// <param name="httpClient">httpClient实例</param>
42-
/// <param name="serviceProvider">服务提供者</param>
43-
/// <param name="httpApiOptions">Api配置选项</param>
44-
/// <exception cref="ArgumentNullException"></exception>
45-
public HttpContext(HttpClient httpClient, IServiceProvider serviceProvider, HttpApiOptions httpApiOptions)
46-
: base(httpClient, serviceProvider, httpApiOptions)
47-
{
48-
var requiredUri = httpApiOptions.HttpHost ?? httpClient.BaseAddress;
49-
this.RequestMessage = new HttpApiRequestMessage(requiredUri, httpApiOptions.UseDefaultUserAgent);
36+
var requiredUri = context.HttpApiOptions.HttpHost ?? context.HttpClient.BaseAddress;
37+
this.RequestMessage = new HttpApiRequestMessage(requiredUri, context.HttpApiOptions.UseDefaultUserAgent);
5038
this.CancellationTokens = new List<CancellationToken>();
5139
}
5240

WebApiClientCore/Microsoft.Extensions/DependencyInjection/HttpApiExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public static IHttpClientBuilder AddHttpApi<THttpApi>(this IServiceCollection se
3535
{
3636
var httpClient = serviceProvider.GetRequiredService<IHttpClientFactory>().CreateClient(name);
3737
var httpApiOptions = serviceProvider.GetRequiredService<IOptionsMonitor<HttpApiOptions>>().Get(name);
38-
return HttpApi.Create<THttpApi>(httpClient, serviceProvider, httpApiOptions);
38+
var httpClientContext = new HttpClientContext(httpClient, serviceProvider, httpApiOptions, name);
39+
return HttpApi.Create<THttpApi>(httpClientContext);
3940
});
41+
4042
return services.AddHttpClient(name);
4143
}
4244

0 commit comments

Comments
 (0)