【问题标题】:Unhandled exception at Grapevine.Interfaces.Server.HttpResponse.SendResponseGrapevine.Interfaces.Server.HttpResponse.SendResponse 处未处理的异常
【发布时间】:2018-05-26 13:15:41
【问题描述】:

我有一个服务器,它监听几个客户端发送信息的HTTP POST。我使用 Grapevine 作为http 服务器,因为方法非常简单,不需要 ASP 的复杂性。

有时我会得到这个随机的

错误

2017-12-12 15:39:25.5642|ERROR|Omnibox_Server.Modelo.HttpServer.Controllers.OpenALPRController|System.Net.HttpListenerException (0x80004005): The I/O operation has been aborted because of either a thread exit or an application request
   at System.Net.HttpRequestStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.IO.StreamReader.ReadBuffer()
   at System.IO.StreamReader.ReadToEnd()
   at Grapevine.Interfaces.Server.HttpRequest.get_Payload()
   at Omnibox_Server.Modelo.HttpServer.Controllers.OpenALPRController.PostPatente(IHttpContext context)

这是

类/方法:

namespace Omnibox_Server.Modelo.HttpServer.Controllers
{
    [RestResource(BasePath = "/openalpr")]
    public class OpenALPRController
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        [RestRoute(HttpMethod = HttpMethod.POST, PathInfo = "/patente")]
        public IHttpContext PostPatente(IHttpContext context)
        {
            try
            {
                context.Response.StatusCode = HttpStatusCode.Ok;
                context.Response.ContentType = ContentType.JSON;
                context.Response.ContentEncoding = Encoding.UTF8;
                var fotoOpenAlpr = JsonConvert.DeserializeObject<FotoOpenALPR>(context.Request.Payload); //<--- exception occurs here? shouldn't try/catch work?
                var ip = context.Request.RemoteEndPoint;
                if (fotoOpenAlpr.agent_uid != null)
                    Task.Run(async () =>
                    {
                        if (fotoOpenAlpr.is_parked) return;
                        await fotoOpenAlpr.ObtenerFoto(ip.Address);
                        try
                        {
                            var foto = new Foto(fotoOpenAlpr);
                            if (foto.IdOmnibox == 0) Logger.Info("Omnibox sin ID con IP " + ip.Address);
                            await foto.Procesar();
                        }
                        catch (Exception e)
                        {
                        }
                    });
                context.Response.SendResponse(HttpStatusCode.Ok); //or maybe exception triggers here?
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
            return context;
        }
    }
}

中产生了一个事件

Windows 日志:

Application: Omnibox Server.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.HttpListenerException
   at System.Net.HttpResponseStream.Write(Byte[], Int32, Int32)
   at Grapevine.Interfaces.Server.HttpResponse.SendResponse(Byte[])
   at Grapevine.Server.HttpResponseExtensions.SendResponse(Grapevine.Interfaces.Server.IHttpResponse, System.String)
   at Grapevine.Server.Router.Route(System.Object)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

事件查看器中的异常日志和窗口日志具有相同的时间戳。

【问题讨论】:

  • 您能告诉我您使用的是什么版本的 Grapevine 吗?
  • 嗨,斯科特。 Grapevine 版本是 NuGet 中的最新版本。 4.1.1。我通过将行 context.Response.SendResponse(HttpStatusCode.Ok); 移动到 try/catch 下方来解决了这个问题。我认为发生的事情是有时 TCP 管道中断并且有效负载损坏/不完整,因此在尝试获取它时会引发异常,并且因为我没有 SendResponse(OK) 在 try/catch 之外引发另一个异常,从而破坏了我的服务器.

标签: c# .net http grapevine


【解决方案1】:

来自 OP:

我通过将行 context.Response.SendResponse(HttpStatusCode.Ok); 移动到 try/catch 下方来解决此问题。我认为发生的事情是有时 TCP 管道中断并且有效负载损坏/不完整,因此在尝试获取它时会引发异常,并且因为我没有 SendResponse(OK) 在 try/catch 之外引发另一个异常,从而破坏了我的服务器.

【讨论】:

  • 正确。顺便说一句,很棒的图书馆,非常感谢您的工作。
猜你喜欢
  • 2011-11-19
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-29
相关资源
最近更新 更多