Skip to content

Commit a6b964d

Browse files
committed
网络请求异常也执行Filter
1 parent 55d2297 commit a6b964d

File tree

3 files changed

+34
-42
lines changed

3 files changed

+34
-42
lines changed

WebApiClientCore/Implementations/ApiRequestExecuter.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,34 @@ private static async Task HandleRequestAsync(ApiRequestContext context)
7777
private static async Task HandleResponseAsync(ApiResponseContext context)
7878
{
7979
// Return特性请求后执行
80-
foreach (var @return in context.ActionDescriptor.Return.Attributes)
80+
var returns = context.ActionDescriptor.Return.Attributes.GetEnumerator();
81+
while (context.ResultStatus == ResultStatus.None && returns.MoveNext())
8182
{
8283
try
8384
{
84-
await @return.OnResponseAsync(context).ConfigureAwait(false);
85+
await returns.Current.OnResponseAsync(context).ConfigureAwait(false);
8586
}
8687
catch (Exception ex)
8788
{
8889
context.Exception = ex;
8990
}
91+
}
9092

91-
if (context.ResultStatus != ResultStatus.None)
93+
// 结果验证
94+
if (context.ResultStatus == ResultStatus.HasResult &&
95+
context.ActionDescriptor.Return.DataType.IsRawType == false &&
96+
context.HttpContext.HttpApiOptions.UseReturnValuePropertyValidate)
97+
{
98+
try
9299
{
93-
break;
100+
DataValidator.ValidateReturnValue(context.Result);
101+
}
102+
catch (Exception ex)
103+
{
104+
context.Exception = ex;
94105
}
95106
}
96107

97-
// 结果验证
98-
DataValidator.ValidateReturnValue(context);
99-
100108
// GlobalFilter请求后执行
101109
foreach (var filter in context.HttpContext.HttpApiOptions.GlobalFilters)
102110
{

WebApiClientCore/Implementations/ApiRequestSender.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ static class ApiRequestSender
1717
/// 发送http请求
1818
/// </summary>
1919
/// <param name="context"></param>
20-
/// <exception cref="HttpRequestException"></exception>
2120
/// <exception cref="ApiInvalidConfigException"></exception>
2221
/// <returns></returns>
2322
public static async Task<ApiResponseContext> SendAsync(ApiRequestContext context)
@@ -27,6 +26,25 @@ public static async Task<ApiResponseContext> SendAsync(ApiRequestContext context
2726
throw new ApiInvalidConfigException(Resx.required_HttpHost);
2827
}
2928

29+
try
30+
{
31+
await SendCoreAsync(context).ConfigureAwait(false);
32+
return new ApiResponseContext(context);
33+
}
34+
catch (Exception ex)
35+
{
36+
return new ApiResponseContext(context) { Exception = ex };
37+
}
38+
}
39+
40+
/// <summary>
41+
/// 发送http请求
42+
/// </summary>
43+
/// <param name="context"></param>
44+
/// <exception cref="HttpRequestException"></exception>
45+
/// <returns></returns>
46+
private static async Task SendCoreAsync(ApiRequestContext context)
47+
{
3048
var actionCache = await context.GetCaheAsync().ConfigureAwait(false);
3149
if (actionCache != null && actionCache.Value != null)
3250
{
@@ -44,7 +62,6 @@ public static async Task<ApiResponseContext> SendAsync(ApiRequestContext context
4462
context.HttpContext.ResponseMessage = response;
4563
await context.SetCacheAsync(actionCache?.Key, response).ConfigureAwait(false);
4664
}
47-
return new ApiResponseContext(context);
4865
}
4966

5067
/// <summary>

WebApiClientCore/Implementations/DataValidator.cs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,6 @@ public static void ValidateParameter(ApiParameterDescriptor parameter, object? p
3939
}
4040
}
4141

42-
/// <summary>
43-
/// 验证参返回的结果
44-
/// </summary>
45-
/// <param name="context">上下文</param>
46-
public static void ValidateReturnValue(ApiResponseContext context)
47-
{
48-
if (context.ActionDescriptor.Return.DataType.IsRawType == true)
49-
{
50-
return;
51-
}
52-
53-
if (context.ResultStatus != ResultStatus.HasResult)
54-
{
55-
return;
56-
}
57-
58-
if (context.HttpContext.HttpApiOptions.UseReturnValuePropertyValidate == false)
59-
{
60-
return;
61-
}
62-
63-
try
64-
{
65-
ValidateReturnValue(context.Result);
66-
}
67-
catch (Exception ex)
68-
{
69-
context.Exception = ex;
70-
}
71-
}
72-
73-
7442
/// <summary>
7543
/// 验证参返回的结果
7644
/// </summary>
@@ -85,7 +53,6 @@ public static void ValidateReturnValue(object? value)
8553
}
8654
}
8755

88-
8956
/// <summary>
9057
/// 返回是否需要进行属性验证
9158
/// </summary>

0 commit comments

Comments
 (0)