ERROR: Asp.net core 3.1 throw "Connection reset by peer".

Dung Do Tien Dec 31 2020 612

In Asp.net core 3.1 when I call ajax to generate a view component for a page. If small traffic it's still working fine. But when traffic is larger I got an exception 'ConnectionResetException: Connection reset by peer'. Detail error is below:

Microsoft.AspNetCore.Connections.ConnectionResetException: Connection reset by peer ---> System.Net.Sockets.SocketException: Connection reset by peer   
    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitableEventArgs.<GetResult>g__ThrowSocketException|7_0(SocketError e)
    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitableEventArgs.GetResult()
    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.ProcessReceives()
    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.DoReceive()
    --- End of inner exception stack trace ---
    at System.IO.Pipelines.PipeCompletion.ThrowLatchedException()
    at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
    at System.IO.Pipelines.Pipe.GetReadAsyncResult()
    at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.PumpAsync()
    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)
    crosoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.StartTimingReadAsync(CancellationToken cancellationToken)
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)
    at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.Read(Byte[] buffer, Int32 offset, Int32 count)
    at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.Read(Byte[] buffer, Int32 offset, Int32 count)
    at System.IO.StreamReader.ReadBuffer()
    at System.IO.StreamReader.ReadToEnd()
    at NLog.Web.LayoutRenderers.AspNetRequestPostedBody.BodyToString(Stream body)
    at NLog.Web.LayoutRenderers.AspNetRequestPostedBody.DoAppend(StringBuilder builder, LogEventInfo logEvent)
    at NLog.LayoutRenderers.LayoutRenderer.RenderAppendBuilder(LogEventInfo logEvent, StringBuilder builder) NLog.ExceptionLoggedToInternalLogger: True
    System.Net.Sockets.SocketException (104): Connection reset by peer
    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitableEventArgs.<GetResult>g__ThrowSocketException|7_0(SocketError e)
    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketAwaitableEventArgs.GetResult()
    crosoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.ProcessReceives()
    at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal.SocketConnection.DoReceive()

This is my action:

[HttpPost]
public IActionResult GenPoll(int questionId)
{
    // Get list Q&A and return view component
    var resultModel = _articleAppService.GetQuestionAnswer(questionId, false);
    return ViewComponent("Poll", resultModel);
}

And this is my ajax call action:

$.ajax({
    url: "/Article/GenPoll",
    type: "POST",
    dataType: "html",
    cache: false,
    data: { 
      questionId: questionId 
    },
    async: true,
    success: function (resultData) {
        // TODO: something here
    }
});

Thanks for any suggestion.

Have 1 answer(s) found.
  • M

    Milan cubes Dec 31 2020

    I also got the same error.

    This error occurs when you use POST method. When you return a view, viewComponent or partialView you should use GET method. So you only change [HttpPost] to [HttpGet] and change type POST to Get in ajax, It will resolve this error for you.

    [HttpGet]
    public IActionResult GenPoll(int questionId)
    {
        var resultModel = _articleAppService.GetQuestionAnswer(questionId, false);
        return ViewComponent("Poll", resultModel);
    }
    $.ajax({
        url: "/Article/GenPoll",
        type: "GET",
        dataType: "html",
        cache: false,
        data: { 
          questionId: questionId 
        },
        async: true,
        success: function (resultData) {
            // TODO: something here
        }
    });

    I hope it's helpful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close