猿问

HttpClient 已断开连接。试图读取流的末尾

我在 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":"   

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


喵喵时光机
浏览 184回答 1
1回答

元芳怎么了

我用谷歌搜索更准确,发现了一个问题。这是已知问题并且已经修复,但不是在当前版本中。我想任何面临此类问题的人都需要构建问题讨论中提到的正确 dll 。
随时随地看视频慕课网APP
我要回答