File tree Expand file tree Collapse file tree 4 files changed +61
-5
lines changed Expand file tree Collapse file tree 4 files changed +61
-5
lines changed Original file line number Diff line number Diff line change 1+ using System . Net . Http ;
2+ using System . Threading . Tasks ;
3+
4+ namespace WebApiClientCore . Attributes
5+ {
6+ /// <summary>
7+ /// 指示请求完成选项的特性
8+ /// </summary>
9+ public class HttpCompletionOptionAttribute : ApiActionAttribute
10+ {
11+ /// <summary>
12+ /// 请求完成选项
13+ /// </summary>
14+ private readonly HttpCompletionOption completionOption ;
15+
16+ /// <summary>
17+ /// 指示请求完成选项的特性
18+ /// </summary>
19+ /// <param name="completionOption">请求完成选项</param>
20+ public HttpCompletionOptionAttribute ( HttpCompletionOption completionOption )
21+ {
22+ this . completionOption = completionOption ;
23+ }
24+
25+ /// <summary>
26+ /// 执行前
27+ /// </summary>
28+ /// <param name="context">上下文</param>
29+ /// <returns></returns>
30+ public override Task OnRequestAsync ( ApiRequestContext context )
31+ {
32+ context . HttpContext . CompletionOption = this . completionOption ;
33+ return Task . CompletedTask ;
34+ }
35+ }
36+ }
Original file line number Diff line number Diff line change @@ -85,7 +85,7 @@ public sealed async override Task OnResponseAsync(ApiResponseContext context)
8585 {
8686 logMessage . HasResponse = true ;
8787 logMessage . ResponseHeaders = response . GetHeadersString ( ) ;
88- logMessage . ResponseContent = await this . ReadResponseContentAsync ( response ) . ConfigureAwait ( false ) ;
88+ logMessage . ResponseContent = await this . ReadResponseContentAsync ( context ) . ConfigureAwait ( false ) ;
8989 }
9090
9191 await this . WriteLogAsync ( context , logMessage ) . ConfigureAwait ( false ) ;
@@ -111,11 +111,25 @@ public sealed async override Task OnResponseAsync(ApiResponseContext context)
111111 /// <summary>
112112 /// 读取响应内容
113113 /// </summary>
114- /// <param name="response "></param>
114+ /// <param name="context "></param>
115115 /// <returns></returns>
116- private async Task < string ? > ReadResponseContentAsync ( HttpResponseMessage response )
116+ private async Task < string ? > ReadResponseContentAsync ( ApiResponseContext context )
117117 {
118- return response . Content == null ? null : await response . Content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
118+ var content = context . HttpContext . ResponseMessage ? . Content ;
119+ if ( content == null )
120+ {
121+ return null ;
122+ }
123+
124+ if ( context . HttpContext . CompletionOption == HttpCompletionOption . ResponseHeadersRead )
125+ {
126+ if ( content . IsBuffered ( ) == false )
127+ {
128+ return "..." ;
129+ }
130+ }
131+
132+ return await content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
119133 }
120134
121135 /// <summary>
Original file line number Diff line number Diff line change @@ -37,8 +37,9 @@ 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 ;
4041 using var tokenLinker = new CancellationTokenLinker ( context . HttpContext . CancellationTokens ) ;
41- var response = await client . SendAsync ( request , tokenLinker . Token ) . ConfigureAwait ( false ) ;
42+ var response = await client . SendAsync ( request , completionOption , tokenLinker . Token ) . ConfigureAwait ( false ) ;
4243
4344 context . HttpContext . ResponseMessage = response ;
4445 await SetCacheAsync ( context , actionCache ? . Key , response ) . ConfigureAwait ( false ) ;
Original file line number Diff line number Diff line change @@ -10,6 +10,11 @@ namespace WebApiClientCore
1010 /// </summary>
1111 public class HttpContext : HttpClientContext , IDisposable
1212 {
13+ /// <summary>
14+ /// 获取或设置指示请求完成选项
15+ /// </summary>
16+ public HttpCompletionOption CompletionOption { get ; set ; }
17+
1318 /// <summary>
1419 /// 获取请求取消令牌集合
1520 /// </summary>
You can’t perform that action at this time.
0 commit comments