【问题标题】:HttpClient has disconnected. Attempted to read past the end of the streamHttpClient 已断开连接。试图读取流的末尾
【发布时间】:2019-01-15 04:03:10
【问题描述】:

我在 IIS 上有一个简单的 webApi 应用程序。有一个 MessageController 是 ControllerBase 和 [Route("api/[controller]"), ApiController]。我有唯一的方法[HttpPost] public IActionResult Send([FromBody] MessageViewModel message) 与两个字段的模型。

然后,我有一个向 webApi 发送消息的 ApiConnector:

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 { }
    }
}

一切都是用 Asp Core 2.2 编写的。当我从 ConsoleApp(while(true) 循环内部)运行 ApiConnector 时,一切正常。当我从邮递员发帖时,一切正常。但是,一旦我尝试从任何其他地方(应用程序,我想没有无限循环)做同样的事情,WebApi 的控制器就会崩溃。

消息:客户端已断开连接; 内部消息:尝试读取流的末尾。

如何正确发送这个请求?


异常文本:

"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

附:我试过Wait()_client的结果无济于事。

【问题讨论】:

  • P.S. exception text will be provided on request.这不是我们做事的方式,无论如何请提供它
  • @TheGeneral,如你所愿

标签: c# .net-core dotnet-httpclient


【解决方案1】:

我用谷歌搜索得更准确,发现了一个问题。这是已知问题并且已经修复,但不是在当前版本中。我想任何遇到此类问题的人都需要构建问题discuss中提到的正确dll。

【讨论】:

    猜你喜欢
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 2013-12-31
    • 1970-01-01
    • 2018-02-01
    • 2020-09-13
    • 2013-08-02
    相关资源
    最近更新 更多