出现异常:指定的网络名称不再可用。 (0x80070040)尝试使用 ReadToEndAsync() 读取 HttpRequest Body 时

2024-04-04

我有一个 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]: 

这是什么原因呢?我读取请求正文的方式不正确吗?我该如何解决这个问题?


这似乎是 ASP.NET Core 中的一个错误,已在 v5 中修复:

https://github.com/dotnet/aspnetcore/pull/25146 https://github.com/dotnet/aspnetcore/pull/25146

https://github.com/dotnet/aspnetcore/issues/25437 https://github.com/dotnet/aspnetcore/issues/25437

读取请求正文可能会引发异常。此更改添加了一些额外的防护措施 为此,并将其呈现为 4xx 响应而不是 5xx 响应。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

出现异常:指定的网络名称不再可用。 (0x80070040)尝试使用 ReadToEndAsync() 读取 HttpRequest Body 时 的相关文章

随机推荐