Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient has disconnected. Attempted to read past the end of the stream

I have a simple webApi app on IIS. There is a MessageController which is ControllerBase and [Route("api/[controller]"), ApiController]. There I have the only method [HttpPost] public IActionResult Send([FromBody] MessageViewModel message) with the model of two fields.

Then, I have an ApiConnector which sends messages to the webApi:

public class ApiConnector : IDisposable
{
    private readonly HttpClient _client = new HttpClient();
    private readonly string _apiUrl;

    public ApiConnector(string apiUrl) => _apiUrl = apiUrl;

    public void SendMessage(string sender, string message)
    {
        try
        {
            _client.PostAsJsonAsync($"{_apiUrl}/message", new { sender, message });
        }
        catch { }
    }
}

Everything is written with Asp Core 2.2. When I run ApiConnector from ConsoleApp (inside of while(true) loop) everything is ok. When I POST from Postman everything is ok. But, as soon as I try to do the same from any other place (app, without endless loop, I suppose) a Controller of WebApi crushes.

Message: The client has disconnected; Inner message: Attempted to read past the end of the stream.

How to send this request properly?


Exception text anyway:

"Message":"The client has disconnected",
"Data":{},
"InnerException":{
   "ClassName":"System.IO.EndOfStreamException",
   "Message":"Attempted to read past the end of the stream.",
   "Data":null,
   "InnerException":null,
   "HelpURL":null,
   "StackTraceString":null,
   "RemoteStackTraceString":null,
   "RemoteStackIndex":0,
   "ExceptionMethod":null,
   "HResult":-2147024858,
   "Source":null,
   "WatsonBuckets":null},
"StackTrace":"   
at Microsoft.AspNetCore.Server.IIS.Core.IO.AsyncIOOperation.GetResult(Int16 token)\r\n   
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadBody()\r\n   
at System.IO.Pipelines.PipeCompletion.ThrowLatchedException()\r\n   
at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)\r\n   
at System.IO.Pipelines.Pipe.GetReadAsyncResult()\r\n   
at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)\r\n   
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadAsync(Memory`1 memory, CancellationToken cancellationToken)\r\n   
at Microsoft.AspNetCore.Server.IIS.Core.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)\r\n   
at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)\r\n   
at Microsoft.AspNetCore.WebUtilities.StreamHelperExtensions.DrainAsync(Stream stream, ArrayPool`1 bytePool, Nullable`1 limit, CancellationToken cancellationToken)\r\n   
at Microsoft.AspNetCore.Mvc.Formatters.JsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)\r\n   
at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(ModelBindingContext bindingContext)\r\n   
at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value)\r\n   
at Microsoft.AspNetCore.Mvc.Internal.ControllerBinderDelegateProvider.<>c__DisplayClass0_0.<<CreateBinderDelegate>g__Bind|0>d.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()\r\n   
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)\r\n   
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()\r\n   
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()\r\n   
at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)\r\n   
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)\r\n   
at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)\r\n   
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()","HelpLink":null,"Source":"Microsoft.AspNetCore.Server.IIS","HResult":-2146232800

P.S. I have tried to Wait() _client's result to no avail.

like image 607
cyberpug2077 Avatar asked Jan 31 '26 18:01

cyberpug2077


1 Answers

I googled more accurate and found an issue. This is known problem and already fixed, but not in a current release. I suppose anyone who face such a problem need to build proper dlls mentioned in the issue discuss.

like image 102
cyberpug2077 Avatar answered Feb 02 '26 08:02

cyberpug2077



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!