我使用Grapevine(这只是HttpListener的接口)创建了一个简单的http端点。有时,连接会在我SendResponse
之前掉线,这会导致HttpListenerException,但我不明白为什么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;
}
答案 0 :(得分:1)
这是一个已知问题。有一个PR可以解决此问题,已经解决了一段时间,我现在将其合并,以及将添加对.NET Standard支持的更新。这应该在周末结束。
答案 1 :(得分:0)
如果SendResponse异步并且您没有等待它,则可能会发生这种情况。