Csharp/C#教程:Owin SelfHost WebApi – 客户端在响应期间关闭连接会引发exception吗?分享


Owin SelfHost WebApi – 客户端在响应期间关闭连接会引发exception吗?

我正在运行一个基于Owin Selfhost的WebApi,我在其中放入了一个API未处理的exception记录器

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

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

我没有深入研究套接字级别发生的事件序列,到目前为止,这只是一个日志记录麻烦。

 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.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.d__20.MoveNext() 

我在Raspberry Pi上与Owin主机有类似的问题。 这可能有助于https://stackoverflow.com/a/30583109/1786034

它的代码问题

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

上述就是C#学习教程:Owin SelfHost WebApi – 客户端在响应期间关闭连接会引发exception吗?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 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; } } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1002629.html

(0)
上一篇 2021年12月28日
下一篇 2021年12月28日

精彩推荐