我正在运行一个基于 Owin Selfhost 的 WebApi,其中我通过以下方式放入了 API 未处理的异常记录器
config.Services.Add(typeof(IExceptionLogger), _apiExceptionLogger);
异常记录器的相关部分:
public override void Log(ExceptionLoggerContext context)
{
if (context == null || context.ExceptionContext == null) return;
Logger.Error("Unhandled exception from Web API", context.ExceptionContext.Exception);
}
它定期捕获和记录的情况是客户端请求数据集,然后在返回结果(JSON)时关闭连接 - 人们在 chrome 中发出请求,然后在所有结果返回之前点击 X 按钮:磷
为了完整起见,我在下面粘贴了堆栈跟踪,只想知道两件事:
- 这是正常/预期的行为吗? AFAIK 是...我正在运行一个非常默认的 API 和管道
- 有什么办法可以处理这个问题吗?本质上,在取消的情况下更优雅地停止请求处理(确实想到了遍布整个请求管道的取消令牌,但在这种情况下看起来它们并没有做太多事情,毕竟所有令牌仅支持合作取消)
我还没有对套接字级别发生的事件序列进行任何深入研究,到目前为止,这只是一个日志记录麻烦。
System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException ---> System.Net.HttpListenerException: The I/O operation has been aborted because of either a thread exit or an application request
at System.Net.HttpResponseStream.EndWrite(IAsyncResult asyncResult)
at Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.EndWrite(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
at Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.EndWrite(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Owin.HttpMessageHandlerAdapter.<SendResponseContentAsync>d__20.MoveNext() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Owin.HttpMessageHandlerAdapter.<SendResponseContentAsync>d__20.MoveNext()