如何从ServiceStack.HttpResponseExtensionsInternal中捕获特殊的ServiceStack异常“ response.FlushAsync”?

问题描述

内部异常是“远程主机关闭了连接。错误代码是0x80070057。”

它们仅发生在产品环境中,这不是我的代码,因此try catch无济于事。

UncaughtExceptionHandlers 也无法捕获它:

 UncaughtExceptionHandlers.Add((req,res,operationName,ex) => {
                if (ex is HttpException && ex.Message.StartsWith("The remote host closed the connection",StringComparison.InvariantCultureIgnoreCase)) {
                    res.EndRequest(true); 
                }
            });

堆栈跟踪:

   at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result,Boolean throwOndisconnect)
   at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
   at System.Web.HttpResponse.Flush(Boolean finalFlush,Boolean async)
   at System.Web.HttpWriter.WriteFromStream(Byte[] data,Int32 offset,Int32 size)
   at System.IO.Stream.<>c.<BeginWriteInternal>b__46_0(Object )
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.Execute()
--- End of stack trace from prevIoUs location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.IO.Stream.EndWrite(IAsyncResult asyncResult)
   at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_1(Stream stream,IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncTrimPromise`1.Complete(TInstance thisRef,Func`3 endMethod,IAsyncResult asyncResult,Boolean requiresSynchronization)
--- End of stack trace from prevIoUs location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.StreamExtensions.<WritetoAsync>d__39.MoveNext()
--- End of stack trace from prevIoUs location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at ServiceStack.BufferedExtensions.<FlushBufferIfAnyAsync>d__4.MoveNext()
--- End of stack trace from prevIoUs location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.Host.AspNet.AspNetResponse.<FlushAsync>d__40.MoveNext()
--- End of stack trace from prevIoUs location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptiondispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at ServiceStack.HttpResponseExtensionsInternal.<WritetoResponse>d__7.MoveNext()
  • loggerName:ServiceStack.HttpResponseExtensionsInternal
  • ServiceStack版本:5.7.0
  • Net框架:4.7.2

它发生了太多次,所以我想忽略它。 内部异步异常处理该如何解决

解决方法

您需要过滤掉ServiceStack.HttpResponseExtensionsInternal中的错误消息,并输入当前的记录器实现。