簡體   English   中英

嘗試/捕獲不捕獲HttpListenerException

[英]Try/catch not catching HttpListenerException

我使用Grapevine(這只是HttpListener的接口)制作了一個簡單的http端點。 有時,連接會在我發送到HttpListenerException的SendResponse之前斷開,但是我不明白為什么try / catch無法處理異常並且整個服務器崩潰。

錯誤:

Application: Movimiento de Placas.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Net.HttpListenerException
Stack:
   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()

碼:

[RestRoute(HttpMethod = HttpMethod.POST, PathInfo = "/patente")]
        public IHttpContext ModificarPantalla(IHttpContext context)
        {
            var dict = HttpUtility.ParseQueryString(context.Request.Payload);
            var json = new JavaScriptSerializer().Serialize(
                dict.Keys.Cast<string>()
                    .ToDictionary(k => k, k => dict[k]));
            var contenido = JsonConvert.DeserializeObject<Patente>(json);
            Server.FormRef.CargarPatente(contenido.Plate, contenido.idCamera);
            UltimaFoto.Fecha = DateTime.Now;
            Task.Run(() => Sqlite.InsertarPatente(contenido));
            try
            {
                context.Response.SendResponse(HttpStatusCode.Ok); //exception occurs here
            }
            catch (Exception ex)
            {

            }

            return context;
        }

這是一個已知的問題。 有一個PR可以解決此問題,已經解決了一段時間,我現在將其合並,以及將添加對.NET Standard支持的更新。 這應該在一周結束時可用。

更新:自2019年8月9日起,Nuget.org上提供Grapevine 4.1.2

如果SendResponse是異步的,而您沒有等待它,則可能會發生這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM