【问题标题】:C# Web API Service - HttpRequestException: Error while copying content to a streamC# Web API 服务 - HttpRequestException:将内容复制到流时出错
【发布时间】:2020-04-10 22:22:45
【问题描述】:

我在 C# 中创建了一个 Web API 服务并将其托管在运行良好的服务器中。 API 服务具有以下特点。

  1. 客户端可以通过“API Controller”发送请求并获得响应
  2. 客户端可以通过 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


【解决方案1】:

你签出CSHARP-1160了吗?

它基本上说您需要将?connect=replicaSet 附加到您的连接字符串。

实际上,我们区分了连接到独立服务器和直接连接到副本集成员,后者相对不常见。不幸的是,MongoLab 的单节点设置实际上是一个单节点副本集,这导致我们不信任它。您可以通过将 ?connect=replicaSet 附加到连接字符串来解决此问题。它将强制驱动程序进入副本集模式,一切都会正常工作。

【讨论】:

  • 你怎么知道 OP 正在使用 mongoDB?
猜你喜欢
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
  • 2018-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多