HttpClient PostAsync() 从不 return 响应
HttpClient PostAsync() never return response
我的问题与这里的 question 非常相似。我有一个 AuthenticationService
class 可以生成 HttpClient
PostAsync()
并且当我 运行 来自 [=29= 时永远不会 returns 结果] 项目,但是当我在控制台应用程序中实现它时,它工作得很好。
这是我的身份验证服务class:
public class AuthenticationService : BaseService
{
public async Task<Token> Authenticate (User user, string url)
{
string json = JsonConvert.SerializeObject(user);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await _client.PostAsync(url, content);
string responseContent = await response.Content.ReadAsStringAsync();
Token token = JsonConvert.DeserializeObject<Token>(responseContent);
return token;
}
}
它就挂在这里:HttpResponseMessage response = await _client.PostAsync(url, content);
这是我的控制器调用服务:
public ActionResult Signin(User user)
{
// no token needed to be send - we are requesting one
Token token = _authenticationService.Authenticate(user, ApiUrls.Signin).Result;
return View();
}
这是我如何使用控制台应用程序测试服务的示例,它运行得很好。
class Program
{
static void Main()
{
AuthenticationService auth = new AuthenticationService();
User u = new User()
{
email = "email@hotmail.com",
password = "password123"
};
Token newToken = auth.Authenticate(u, ApiUrls.Signin).Result;
Console.Write("Content: " + newToken.user._id);
Console.Read();
}
}
由于您使用的是 .Result
,这最终会导致您的代码出现死锁。这在控制台应用程序中起作用的原因是因为控制台应用程序没有上下文,但 ASP.NET 应用程序有(请参阅 Stephen Cleary's Don't Block on Async Code)。您应该在控制器 async
中使用 Signin
方法,并 await
调用 _authenticationService.Authenticate
来解决死锁问题。
万一有人过来需要看代码,我只需将控制器更改为如下所示:
/***
*** Added async and Task<ActionResult>
****/
public async Task<ActionResult> Signin(User user)
{
//no token needed - we are requesting one
// added await and remove .Result()
Token token = await _authenticationService.Authenticate(user, ApiUrls.Signin);
return RedirectToAction("Index", "Dashboard", token.user);
}
感谢大家的快速回复!
由于您使用的是 .Result
或 .Wait
或 await
,这最终会导致您的代码出现 死锁。
您可以在 async
方法中使用 ConfigureAwait(false)
来 防止死锁
像这样:
string responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
you can use ConfigureAwait(false)
wherever possible for Don't Block Async Code .
我的问题与这里的 question 非常相似。我有一个 AuthenticationService
class 可以生成 HttpClient
PostAsync()
并且当我 运行 来自 [=29= 时永远不会 returns 结果] 项目,但是当我在控制台应用程序中实现它时,它工作得很好。
这是我的身份验证服务class:
public class AuthenticationService : BaseService
{
public async Task<Token> Authenticate (User user, string url)
{
string json = JsonConvert.SerializeObject(user);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await _client.PostAsync(url, content);
string responseContent = await response.Content.ReadAsStringAsync();
Token token = JsonConvert.DeserializeObject<Token>(responseContent);
return token;
}
}
它就挂在这里:HttpResponseMessage response = await _client.PostAsync(url, content);
这是我的控制器调用服务:
public ActionResult Signin(User user)
{
// no token needed to be send - we are requesting one
Token token = _authenticationService.Authenticate(user, ApiUrls.Signin).Result;
return View();
}
这是我如何使用控制台应用程序测试服务的示例,它运行得很好。
class Program
{
static void Main()
{
AuthenticationService auth = new AuthenticationService();
User u = new User()
{
email = "email@hotmail.com",
password = "password123"
};
Token newToken = auth.Authenticate(u, ApiUrls.Signin).Result;
Console.Write("Content: " + newToken.user._id);
Console.Read();
}
}
由于您使用的是 .Result
,这最终会导致您的代码出现死锁。这在控制台应用程序中起作用的原因是因为控制台应用程序没有上下文,但 ASP.NET 应用程序有(请参阅 Stephen Cleary's Don't Block on Async Code)。您应该在控制器 async
中使用 Signin
方法,并 await
调用 _authenticationService.Authenticate
来解决死锁问题。
万一有人过来需要看代码,我只需将控制器更改为如下所示:
/***
*** Added async and Task<ActionResult>
****/
public async Task<ActionResult> Signin(User user)
{
//no token needed - we are requesting one
// added await and remove .Result()
Token token = await _authenticationService.Authenticate(user, ApiUrls.Signin);
return RedirectToAction("Index", "Dashboard", token.user);
}
感谢大家的快速回复!
由于您使用的是 .Result
或 .Wait
或 await
,这最终会导致您的代码出现 死锁。
您可以在 async
方法中使用 ConfigureAwait(false)
来 防止死锁
像这样:
string responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
you can use
ConfigureAwait(false)
wherever possible for Don't Block Async Code .