Asp.net core ASP NET Core中PostAsync异常的自定义InputFormatter
我有一个奇怪的问题,我不知道为什么会有问题: 在客户端,我有以下代码:Asp.net core ASP NET Core中PostAsync异常的自定义InputFormatter,asp.net-core,Asp.net Core,我有一个奇怪的问题,我不知道为什么会有问题: 在客户端,我有以下代码: HttpResponseMessage response = await _httpClient.PostAsync("http://127.0.0.1:5544/api/Blablabla", new StringContent("test")); 在服务器上,我实现了一个自定义InputFormatter,它具有 public async override Task<InputFormatterResult&
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();
}
}
一切正常
有什么问题吗?好的,我修好了
问题是,正如您可以想象的那样,调用postasync的函数是异步的,但之前的函数不是,我没有对该函数进行.Wait()
与asp net核心无关,只是同步代码中的异步代码问题
HttpResponseMessage response = _httpClient.PostAsync("http://127.0.0.1:5544/api/Blablabla", new StringContent("test")).Result;