Getting Exception: The specified network name is no longer available. (0x80070040) when trying to read HttpRequest Body using ReadToEndAsync()

0

I have an API post web method that tries to get the request body out using StreamReader ReadToEndAsync(). The majority of the time it works fine, however for very random request I am getting the following exception:

The client has disconnected.

with inner exception

The specified network name is no longer available. (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();
    }
}

Full Exception

[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]: 

What is the cause for this? Is the way that I am reading the request body not correct? How can I resolve this?

c#
asp.net-core
asked on Stack Overflow Oct 20, 2020 by vuvu27 • edited Oct 20, 2020 by Roman Marusyk

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0