我在IIS上有一个简单的webApi应用程序。有一个MessageController
,它是ControllerBase和[Route("api/[controller]"), ApiController]
。在那里,我只有两个域模型的方法[HttpPost] public IActionResult Send([FromBody] MessageViewModel message)
。
然后,我有一个ApiConnector,它将消息发送到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 { }
}
}
一切都用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
P.S。我试图Wait()
_client的结果无济于事。