我有一个奇怪的问题,我不知道为什么会给出问题:
在客户端,我有这样的代码:
HttpResponseMessage response = await _httpClient.PostAsync("http://127.0.0.1:5544/api/Blablabla", new StringContent("test"));在服务器上,我实现了一个定制的InputFormatter,它具有以下内容
public async override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
var request = context.HttpContext.Request;
try
{
using (var reader = new StreamReader(request.Body))
{
var content = await reader.ReadToEndAsync().ConfigureAwait(false);
return await InputFormatterResult.SuccessAsync(content).ConfigureAwait(false);
}
}
catch (Exception e)
{
return await InputFormatterResult.FailureAsync();
}
}如果我尝试这样做,服务器的catch异常就会被触发,这会给我一个异常:
现有连接已被远程主机强制关闭
但是..。
如果在客户端,我让PostAsync“同步”这样做:
HttpResponseMessage response = _httpClient.PostAsync("http://127.0.0.1:5544/api/Blablabla", new StringContent("test")).Result;一切正常。
有什么问题吗?
发布于 2018-04-26 15:54:05
好吧,我把它修好了……
问题是,正如您可以想象的那样,调用postasync的函数是异步的,但前面的函数不是异步的,并且我没有对那个函数执行.Wait()。
与asp net core无关,只是同步代码的异步代码问题。
https://stackoverflow.com/questions/50027854
复制相似问题