Skip to content

Commit bd8ecc1

Browse files
committed
HttpContext.CompletionOption变更为可空类型
1 parent e7ad2f2 commit bd8ecc1

File tree

6 files changed

+26
-7
lines changed

6 files changed

+26
-7
lines changed

App/Clients/IUserApi.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public interface IUserApi : IHttpApi
3939
[HttpGet("api/users/{account}")]
4040
ITask<byte[]> GetAsByteArrayAsync([Required] string account, CancellationToken token = default);
4141

42-
[HttpGet("api/users/{account}")]
43-
[HttpCompletionOption(HttpCompletionOption.ResponseHeadersRead)]
42+
[HttpGet("api/users/{account}")]
4443
ITask<Stream> GetAsStreamAsync([Required] string account, CancellationToken token = default);
4544

4645
[HttpGet("api/users/{account}")]

WebApiClientCore/ApiRequestContext.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace WebApiClientCore
1+
using System.Net.Http;
2+
3+
namespace WebApiClientCore
24
{
35
/// <summary>
46
/// 表示Api请求的上下文
@@ -50,5 +52,22 @@ protected ApiRequestContext(HttpContext httpContext, ApiActionDescriptor apiActi
5052
this.Arguments = arguments;
5153
this.Properties = properties;
5254
}
55+
56+
/// <summary>
57+
/// 返回请求使用的HttpCompletionOption
58+
/// </summary>
59+
/// <returns></returns>
60+
public HttpCompletionOption GetCompletionOption()
61+
{
62+
if (this.HttpContext.CompletionOption != null)
63+
{
64+
return this.HttpContext.CompletionOption.Value;
65+
}
66+
67+
var dataType = this.ApiAction.Return.DataType;
68+
return dataType.IsRawHttpResponseMessage || dataType.IsRawStream
69+
? HttpCompletionOption.ResponseHeadersRead
70+
: HttpCompletionOption.ResponseContentRead;
71+
}
5372
}
5473
}

WebApiClientCore/Attributes/ActionAttributes/HttpCompletionOptionAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace WebApiClientCore.Attributes
66
{
77
/// <summary>
88
/// 指示请求完成选项的特性
9+
/// 缺省的情况下,当声明返回类型为Stream或HttpResponseMessage时使用ResponseHeadersRead
910
/// </summary>
1011
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
1112
public class HttpCompletionOptionAttribute : ApiActionAttribute

WebApiClientCore/Attributes/FilterAttributes/LoggingFilterAttribute.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ public sealed async override Task OnResponseAsync(ApiResponseContext context)
121121
return null;
122122
}
123123

124-
if (content.IsBuffered() == true ||
125-
context.HttpContext.CompletionOption == HttpCompletionOption.ResponseContentRead)
124+
if (content.IsBuffered() == true || context.GetCompletionOption() == HttpCompletionOption.ResponseContentRead)
126125
{
127126
return await content.ReadAsStringAsync().ConfigureAwait(false);
128127
}

WebApiClientCore/BuildInProxies/Invokers/HttpRequest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public static async Task<ApiResponseContext> SendAsync(ApiRequestContext context
3737
{
3838
var client = context.HttpContext.HttpClient;
3939
var request = context.HttpContext.RequestMessage;
40-
var completionOption = context.HttpContext.CompletionOption;
40+
var completionOption = context.GetCompletionOption();
41+
4142
using var tokenLinker = new CancellationTokenLinker(context.HttpContext.CancellationTokens);
4243
var response = await client.SendAsync(request, completionOption, tokenLinker.Token).ConfigureAwait(false);
4344

WebApiClientCore/HttpContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class HttpContext : HttpClientContext, IDisposable
1313
/// <summary>
1414
/// 获取或设置指示请求完成选项
1515
/// </summary>
16-
public HttpCompletionOption CompletionOption { get; set; }
16+
public HttpCompletionOption? CompletionOption { get; set; }
1717

1818
/// <summary>
1919
/// 获取请求取消令牌集合

0 commit comments

Comments
 (0)