C#Web API服务-HttpRequestException:将内容复制到流时出错

问题描述 投票:0回答:1

我已经用C#提供了Web API服务,并将其托管在运行了一段时间的服务器中。 API服务具有以下功能。

  1. 客户端可以通过“ API控制器”发送请求并获得响应
  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(Byte []缓冲区,Int32偏移量,Int32计数,AsyncCallback回调,对象状态)---内部异常堆栈跟踪的结尾---Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.BeginRead(Byte []缓冲区,Int32偏移量,Int32计数,AsyncCallback回调,对象状态)在System.Net.Http.StreamToStreamCopy.StartRead()---结束内部异常堆栈跟踪的信息----内部异常堆栈的结尾跟踪---在System.Threading.Tasks.Task`1.GetResultCore(BooleanwaitCompletionNotification)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(Byte []缓冲区,Int32偏移量,Int32计数,AsyncCallback回调,对象状态)---内部异常堆栈跟踪的结尾---Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.BeginRead(Byte []缓冲区,Int32偏移量,Int32计数,AsyncCallback回调,对象状态)在System.Net.Http.StreamToStreamCopy.StartRead()---结束内部异常堆栈跟踪---

answers的大部分都指向客户区,但对我来说,它似乎在Api服务器中

问题在一天内发生了三次,我不确定根本原因。这里有人熟悉这个问题吗?

在服务器上,我有以下一行内容可以读取整个请求正文,这里有什么问题吗?

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;
    }
  }
c# asp.net .net asp.net-web-api httplistener
1个回答
0
投票

您是否已签出CSHARP-1160

基本上说,您需要在连接字符串后附加?connect=replicaSet

有效地,我们在连接到独立服务器和直接连接到副本集成员之间做了区分,后者相对较少见。不幸的是,MongoLab的“单节点”设置实际上是一个单节点副本集,这使我们不信任它。您可以通过在连接字符串后面附加?connect = replicaSet来解决此问题。这将迫使驱动程序进入副本设置模式,并且所有驱动程序都可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.