Skip to content

Commit 5ee97ce

Browse files
committed
opt: 移除不合理的MiddlewareExceptionCaptured设计,此逻辑可以由管道中间件实现;
1 parent 7bd8705 commit 5ee97ce

File tree

3 files changed

+4
-29
lines changed

3 files changed

+4
-29
lines changed

src/Cuture.AspNetCore.ResponseAutoWrapper/Cuture.AspNetCore.ResponseAutoWrapper.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<IsPackable>true</IsPackable>
1717
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1818

19-
<Version>1.2.3</Version>
19+
<Version>1.2.4</Version>
2020
<Description>Response and exception automatic wrapper for `asp.net core` to provide a consistent response content format for `Action`. 用于`asp.net core`的响应和异常自动包装器,使`Action`提供一致的响应内容格式。</Description>
2121

2222
<PackageIdPrefix>Cuture.AspNetCore.ResponseAutoWrapper</PackageIdPrefix>

src/Cuture.AspNetCore.ResponseAutoWrapper/ResponseAutoWrapMiddleware.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ internal class ResponseAutoWrapMiddleware
2626

2727
private readonly ILogger _logger;
2828

29-
private readonly ResponseAutoWrapMiddlewareOptions.MiddlewareExceptionCaptured _middlewareExceptionCaptured;
30-
3129
private readonly RequestDelegate _next;
3230

3331
private readonly bool _notCatchExceptions;
@@ -92,7 +90,6 @@ public ResponseAutoWrapMiddleware(RequestDelegate next,
9290
_notCatchExceptions = !options.CatchExceptions;
9391
_throwCaughtExceptions = options.ThrowCaughtExceptions;
9492
_ignoreOptionsRequest = options.IgnoreOptionsRequest;
95-
_middlewareExceptionCaptured = options.OnMiddlewareExceptionCaptured ?? LogMiddlewareException;
9693

9794
var delegateCollection = GetService<ResponseAutoWrapperWorkDelegateCollection>();
9895

@@ -126,8 +123,6 @@ public async Task InvokeAsync(HttpContext context)
126123
throw;
127124
}
128125

129-
var doesExceptionWrapped = false;
130-
131126
//响应未开始,则包装响应
132127
if (!context.Response.HasStarted
133128
&& !context.RequestAborted.IsCancellationRequested)
@@ -136,7 +131,6 @@ public async Task InvokeAsync(HttpContext context)
136131
if (response is not null)
137132
{
138133
await WriteResponseWithFormatterAsync(context, response);
139-
doesExceptionWrapped = true;
140134
}
141135
}
142136
else //无法对响应进行包装,此时强制向上层抛出异常
@@ -150,7 +144,7 @@ public async Task InvokeAsync(HttpContext context)
150144
}
151145
else //不抛出异常时,触发回调
152146
{
153-
_middlewareExceptionCaptured(context.Request, ex, doesExceptionWrapped);
147+
LogMiddlewareException(context.Request, ex);
154148
}
155149
}
156150
finally
@@ -178,8 +172,7 @@ public async Task InvokeAsync(HttpContext context)
178172
/// </summary>
179173
/// <param name="request">出现异常的请求</param>
180174
/// <param name="exception">异常</param>
181-
/// <param name="hasExceptionWrapped">异常是否已包装</param>
182-
private void LogMiddlewareException(HttpRequest request, Exception exception, in bool hasExceptionWrapped)
175+
private void LogMiddlewareException(HttpRequest request, Exception exception)
183176
{
184177
//https://github.com/dotnet/aspnetcore/tree/8dd33378697e6f8ca89116170ec3046c185724b6/src/Hosting/Hosting/src/Internal/HostingRequestStartingLog.cs
185178
_logger.LogError(exception, "Request error {Protocol} {Method} {Scheme}://{Host}{PathBase}{Path}{QueryString} {ContentType} {ContentLength}",

src/Cuture.AspNetCore.ResponseAutoWrapper/ResponseAutoWrapMiddlewareOptions.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,14 @@
11
using System;
22
using System.Linq;
3-
using Microsoft.AspNetCore.Http;
43
using Microsoft.AspNetCore.Mvc.Formatters;
54

65
namespace Cuture.AspNetCore.ResponseAutoWrapper;
76

87
/// <summary>
9-
///
8+
/// 自动响应包装中间件选项
109
/// </summary>
1110
public class ResponseAutoWrapMiddlewareOptions
1211
{
13-
#region Public 委托
14-
15-
/// <summary>
16-
/// 中间件异常已捕获
17-
/// </summary>
18-
/// <param name="request">出现异常的请求</param>
19-
/// <param name="exception">异常</param>
20-
/// <param name="doesExceptionWrapped">异常是否已包装</param>
21-
public delegate void MiddlewareExceptionCaptured(HttpRequest request, Exception exception, in bool doesExceptionWrapped);
22-
23-
#endregion Public 委托
24-
2512
#region Public 属性
2613

2714
/// <summary>
@@ -44,11 +31,6 @@ public class ResponseAutoWrapMiddlewareOptions
4431
/// </summary>
4532
public bool IgnoreOptionsRequest { get; set; } = true;
4633

47-
/// <summary>
48-
/// 在中间件已捕获异常时的回调
49-
/// </summary>
50-
public MiddlewareExceptionCaptured? OnMiddlewareExceptionCaptured { get; set; }
51-
5234
/// <summary>
5335
/// 是否将捕获到的异常抛出给上层中间件<para/>
5436
/// default is <see langword="false"/>

0 commit comments

Comments
 (0)