Grapevine.Interfaces.Server.HttpResponse.SendResponse中未处理的异常

时间:2017-12-12 18:52:04

标签: c# .net http grapevine

我有一个服务器,它监听多个客户端发送信息的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()

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

1 个答案:

答案 0 :(得分:1)

来自OP:

  

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