【发布时间】:2015-12-03 23:32:27
【问题描述】:
我的问题与这里的question 非常相似。我有一个 AuthenticationService 类,它生成一个 HttpClient PostAsync() 并且当我从 ASP 项目运行它时从不返回结果,但是当我在控制台应用程序中实现它时它工作得很好。
这是我的身份验证服务类:
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();
}
}
【问题讨论】:
标签: c# asp.net asynchronous httpclient