【问题标题】:Owin SelfHost WebApi - client closing the connection during response raises an exception?Owin SelfHost WebApi - 客户端在响应期间关闭连接会引发异常?
【发布时间】:2015-07-18 12:13:54
【问题描述】:

我正在运行一个基于 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

为了完整起见,我在下面粘贴了一个堆栈跟踪,只是想知道两件事:

  • 这是常规/预期行为吗? 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()

【问题讨论】:

  • 您很幸运能够在它发生时识别出这种情况,但我得到了同样的异常(具有相同的堆栈跟踪),没有任何明显的原因。它只是在不同的工作线程中不时发生。它不影响任何想法,客户端得到响应正常。
  • 你不是在等待什么吗?

标签: c# .net asp.net-web-api owin


【解决方案1】:

代码问题

异步读取文件并检查。任何时候如果你正在做一个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;
                    }
                }
            }

【讨论】:

    【解决方案2】:

    我在 Raspberry Pi 上使用 Owin 主机时遇到了类似的问题。这可能会有所帮助https://stackoverflow.com/a/30583109/1786034

    【讨论】:

    • 感谢您查看并回复。但是管道本身并没有中断,一切都运行良好。我在 Owin 提供的未处理异常钩子中捕获并记录这些,所以不需要吞下和记录来保持管道运行。我想那时没有简单的答案,客户端断开连接作为异常传播出 HttpListener :(
    • 你可以试试Nowin server (github.com/Bobris/Nowin)。它显然不使用 HttpListener 所以它可能不会受到这个问题的影响。但是,它还没有准备好投入生产。
    猜你喜欢
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    相关资源
    最近更新 更多