【发布时间】:2020-04-10 22:22:45
【问题描述】:
我在 C# 中创建了一个 Web API 服务并将其托管在运行良好的服务器中。 API 服务具有以下特点。
- 客户端可以通过“API Controller”发送请求并获得响应
- 客户端可以通过 Signalr 连接到 Socket,客户端将与数据一起流式传输
最近几天,我随机遇到一些 API 服务问题,在特定时间段内套接字突然断开连接。当我看到 web api 日志时,我收到以下错误。
System.AggregateException:发生一个或多个错误。 ---> System.Net.Http.HttpRequestException:将内容复制到 溪流。 ---> System.IO.IOException ---> System.Net.HttpListenerException:尝试对 不存在的网络连接 System.Net.HttpRequestStream.BeginRead(Byte[] 缓冲区,Int32 偏移量, Int32 大小,AsyncCallback 回调,对象状态)在 Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.BeginRead(字节[] 缓冲区、Int32 偏移量、Int32 计数、AsyncCallback 回调、对象 状态)---内部异常堆栈跟踪结束---在 Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.BeginRead(字节[] 缓冲区、Int32 偏移量、Int32 计数、AsyncCallback 回调、对象 状态)在 System.Net.Http.StreamToStreamCopy.StartRead() --- 结束 内部异常堆栈跟踪 --- --- 内部异常堆栈结束 跟踪 --- 在 System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)在 ApiServices.Controllers.Streaming.StreamSubs.ListToolbar() --->(内部异常 #0)System.Net.Http.HttpRequestException:将内容复制到流时出错。 ---> System.IO.IOException ---> System.Net.HttpListenerException:尝试对 不存在的网络连接 System.Net.HttpRequestStream.BeginRead(Byte[] 缓冲区,Int32 偏移量, Int32 大小,AsyncCallback 回调,对象状态)在 Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.BeginRead(字节[] 缓冲区、Int32 偏移量、Int32 计数、AsyncCallback 回调、对象 状态)---内部异常堆栈跟踪结束---在 Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.BeginRead(字节[] 缓冲区、Int32 偏移量、Int32 计数、AsyncCallback 回调、对象 状态)在 System.Net.Http.StreamToStreamCopy.StartRead() --- 结束 内部异常堆栈跟踪---
大部分answers 指向客户区,但对我来说似乎在 Api Server 中
问题在一天之内发生了三次,我不确定根本原因。这里有熟悉这个问题的人吗?
在服务器上,我有以下行来读取整个请求正文,这里有什么问题吗?
var task = Request.Content.ReadAsStringAsync();
在课堂上
[RoutePrefix("api/Stream/{actionType}/{user}")]
public class StreamSubs : ApiController
{
[Route("Justs"), HttpPost]
public int SubTokens(string user, string actionType)//, [FromBody] List<int> tokens)
{
var tokens = ReadRequestContent(user);
if (tokens == null)
return -1;
DoSubs(user, actionType, tokens);
return 1;
}
internal List<int> ReadRequestContent(string user)
{
var liststr = "";
try
{
var task = Request.Content.ReadAsStringAsync();
var delay = Task.Delay(TimeSpan.FromSeconds(Utils.HttpReadRequestContentTimeoutInSeconds));
var timeoutTask = Task.WhenAny(task, delay);
if (timeoutTask.Result != task)
{
Log.Trace("StreamSub --- {0} --- Read request content timeout", _streamType);
return null;
}
var tstr = task.Result;
var len = Request.Content.Headers.ContentLength;
if (len != null && len != tstr.Length)
{
Log.Trace("{2} subs length mismatch. HL: {0}, DL: {1}", len, tstr.Length, _streamType);
return null;
}
liststr = tstr;
if (tstr != null)
{
if (tstr.Length > 0)
{
if (!tstr.Contains("null"))
{
try
{
//process the string
}
catch (JsonSerializationException jexp1) { }
catch (JsonReaderException jexp2) { }
catch
{
Log.Error("Exception in ReadRequestContentIndices() - INT- Tokens List - " + liststr);
}
}
}
}
}
catch (Exception e)
{
//if (DateTime.Now.TimeOfDay <= System.TimeSpan.Parse("14:30:00"))
//{
Log.Error("Exception in ReadRequestContent() - INT - Tokens List - " + liststr);
//}
Log.Error(e);
}
return null;
}
}
【问题讨论】:
-
在
Request.Content.ReadAsStringAsync()附近显示代码 -
该错误表示没有从客户端到服务器的路由。服务器未运行或 Route 和/或 RoutePrefix 错误。
标签: c# asp.net .net asp.net-web-api httplistener