繁体   English   中英

C#:无法将数据写入传输连接:使用 RestClient 上传较大文件时损坏 Pipe

[英]C#: Unable to write data to the transport connection: Broken Pipe when using RestClient to upload larger files

我正在使用 RestSharp restClient 上传文件,但出现错误:

 System.Net.WebException: Error while copying content to a stream.
       ---> System.Net.Http.HttpRequestException: Error while copying content to a stream.
       ---> System.IO.IOException: Unable to write data to the transport connection: Broken pipe.
       ---> System.Net.Sockets.SocketException (32): Broken pipe

此错误仅在尝试上传较大文件时发生。 我不知道确切的截止日期,但对于最大 ~29 MB 的文件我没有收到此错误,但对于 37 MB 的文件我确实收到此错误。

客户端代码是:

RestClient client = new("http://data-target-service:15012")
{
  Timeout = -1
};
RestRequest request = new(Method.Post);
request.AddFile("formFile", file.FullName); //file is of type fileInfo

IRestResponse response = await client.ExecuteAsync(request);

这些都是部署在 Kube.netes 上相同命名空间中的服务,因此是 HTTP。被调用的 API 是:

[HttpPost]
public async Task<IActionResult> UploadFile(IFormFile formFile)
{
  try
  {
    await HandleDataHere(formFile);
    return StatusCode(200);
  }
  catch (Exception e)
  {
    Console.Writeline(e.message);
    return StatusCode(500);
  }
}

HandleDataHere方法实际上从未被调用过,因此错误可能不存在。 我在其他地方读到,当多个调用共享同一个 HTTP 客户端时可能会出现问题,因此我让每个调用都使用自己的客户端。

在这一点上,我不确定这是客户端问题,API,还是入口配置问题(由 NGINX 处理)。 任何想法都会非常有帮助。

因此,解决方案是简单地将[DisableRequestSizeLimit]属性添加到 API,并且它有效。

运行 API 到 PostMan 时,错误消息不同:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-55ecaadcbc6dddd7d113966bfebe4d0f-760b8ab8f5082583-00",
    "errors": {
        "": [
            "Failed to read the request form. Request body too large. The max request body size is 30000000 bytes."
        ]
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM