【发布时间】:2020-10-20 23:01:17
【问题描述】:
我有一个 API post web 方法,它尝试使用 StreamReader ReadToEndAsync() 获取请求正文。大多数情况下它工作正常,但是对于非常随机的请求,我得到以下异常:
客户端已断开连接。
内部异常
指定的网络名称不再可用。 (0x80070040)。
[HttpPost]
[Route("api/Logger/Log")]
public async Task<IActionResult> Log()
{
try
{
HttpHelper httpHelper = new HttpHelper();
var requestBody = await httpHelper.GetRequestBody(Request);
return Ok();
}
catch (Exception ex)
{
_logger.LogException(ex);
return StatusCode(500);
}
}
public async Task<string> GetRequestBody(HttpRequest request)
{
using (var reader = new StreamReader(request.Body))
{
return await reader.ReadToEndAsync();
}
}
完全例外
[Parameters]: {}
[Exception Message]: The client has disconnected
[StackTrace]: at Microsoft.AspNetCore.Server.IIS.Core.IO.AsyncIOOperation.GetResult(Int16 token)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadBody()
at System.IO.Pipelines.PipeCompletion.ThrowLatchedException()
at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
at System.IO.Pipelines.Pipe.ReadAsync(CancellationToken token)
at System.IO.Pipelines.Pipe.DefaultPipeReader.ReadAsync(CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadAsync(Memory`1 memory, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.IIS.Core.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)
at System.IO.StreamReader.ReadBufferAsync(CancellationToken cancellationToken)
at System.IO.StreamReader.ReadToEndAsyncInternal()
at OnlineBookingSystem.Utilities.HttpHelper.GetRequestBody(HttpRequest request)
at OnlineBookingSystem.Controllers.LoggingController.Log()
[Inner Exception]: The specified network name is no longer available. (0x80070040)
[Inner StackTrace]:
这是什么原因?我阅读请求正文的方式不正确吗?我该如何解决这个问题?
【问题讨论】:
-
我在 .netcore 3.1 本地运行相同,它一直运行良好。你能分享它在哪种情况下失败?
-
你可以看看这个link。
-
@AjeetKumar 感谢您的回复。这就是问题所在,我不知道在什么情况下它实际上会失败。但它肯定会失败,因为它正在注销异常。该应用程序当前是通过 IIS 发布的,并且大多数情况下它可以正常工作,但它会随机抛出该异常。
标签: c# asp.net-core