HttpClient 已断开连接。试图读取流的末尾
HttpClient has disconnected. Attempted to read past the end of the stream
我在 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 的 ApiConnector(在 while(true)
循环内)时,一切正常。当我从 Postman POST 时,一切正常。但是,一旦我尝试从任何其他地方(应用程序,我想没有无限循环)做同样的事情,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
P.S。我已经尝试 Wait()
_client 的结果无济于事。
我用谷歌搜索更准确,发现了一个问题。这是已知问题并已修复,但当前版本中未修复。我想遇到此类问题的任何人都需要构建问题 discuss.
中提到的适当的 dll
我在 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 的 ApiConnector(在 while(true)
循环内)时,一切正常。当我从 Postman POST 时,一切正常。但是,一旦我尝试从任何其他地方(应用程序,我想没有无限循环)做同样的事情,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
P.S。我已经尝试 Wait()
_client 的结果无济于事。
我用谷歌搜索更准确,发现了一个问题。这是已知问题并已修复,但当前版本中未修复。我想遇到此类问题的任何人都需要构建问题 discuss.
中提到的适当的 dll