Owin SelfHost WebApi - 客户端在响应期间关闭连接会引发异常?

15
我正在运行基于Owin Selfhost的WebApi,并通过API添加了未处理异常记录器。
config.Services.Add(typeof(IExceptionLogger), _apiExceptionLogger);

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按钮:P

为了完整起见,我已经粘贴了下面的堆栈跟踪,只想知道两件事:

  • 这是正常/预期的行为吗?据我所知...我正在运行一个相当默认的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()

你很幸运能够在条件发生时识别出它,但我却无法找到任何明显的原因,就会收到相同的异常(带有相同的堆栈跟踪)。这只是偶尔在不同的工作线程中发生。虽然它并不影响任何事情,但客户端确实收到了正常的响应。 - greatvovan
你难道不在等待某件事情吗? - David McEleney
2个回答

4

感谢您查看并回复。但是,管道本身并没有中断,一切都运行正常。我在Owin提供的未处理异常钩子中捕获和记录这些异常,因此不需要吞咽和记录以保持管道运行。我想没有简单的答案,客户端断开连接会作为异常传播出HttpListener :( - Vivek
你可以尝试使用Nowin服务器(https://github.com/Bobris/Nowin)。它显然不使用HttpListener,因此可能不会受到这个问题的影响。但是,它还没有准备好用于生产。 - zmechanic

0

这是代码问题

异步读取文件并进行检查。如果您正在进行 API 调用并且超时,它肯定会抛出异常。

using (StreamReader reader = new StreamReader(await selectedFile.OpenStreamForReadAsync()))
            {
                while ((nextLine = await reader.ReadLineAsync()) != null)
                {
                    contents.AppendFormat("{0}. ", lineCounter);
                    contents.Append(nextLine);
                    contents.AppendLine();
                    lineCounter++;
                    if (lineCounter > 3)
                    {
                        contents.AppendLine("Only first 3 lines shown.");
                        break;
                    }
                }
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接