C# 为什么web服务任务被取消了?我如何找到答案?
当我尝试使用我编写的在线web服务登录到我的网站时,我根本看不到任何事情发生 我应该指出,当我从localhost运行所有东西时,这是100%有效的 在我的登录中:C# 为什么web服务任务被取消了?我如何找到答案?,c#,.net,asp.net-web-api2,C#,.net,Asp.net Web Api2,当我尝试使用我编写的在线web服务登录到我的网站时,我根本看不到任何事情发生 我应该指出,当我从localhost运行所有东西时,这是100%有效的 在我的登录中: private async Task DoLogin(string EmailAddress, string Password) { using (var client = new HttpClient()) { client.BaseAddress = new
private async Task DoLogin(string EmailAddress, string Password)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(ServiceURL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
User u = new User() { EmailAddress = EmailAddress, Password = Password };
HttpResponseMessage response = await client.PostAsJsonAsync("api/user/login", u);
if (response.IsSuccessStatusCode)
{
u = await response.Content.ReadAsAsync<User>();
if (u.Role.RoleName == "Admin")
{
generateCookie(u);
}
else
{
throw new InvalidOperationException("Your user account is not permitted to access this website");
}
}
else
{
throw new InvalidOperationException("The email address or password you entered is incorrect.");
}
}
}
您可能应该在服务调用中的代码周围放置一个try-catch,并记录/调试抛出的任何异常,以确定代码中有哪些失败。服务方法上的Async将对您隐藏真正的异常,因此您需要在服务内部进行调试,以了解真正发生的情况。@CodeUnique当我在本地测试web应用程序并将其连接到live web服务时,我使用Fiddler4查看发生了什么。起初,我得到一个错误,说web服务无法连接到数据库,但现在我没有得到任何东西。我倾向于发现,即使代码中存在异常,我也没有从异步调用中得到任何回报。异常并没有通过返回进入调用代码,它只是看起来被吞没了。当我在服务中放置一个try-catch块,在catch-low中放置一个break-point时,我看到异常正在触发,这只是一个想法
[HttpPost]
[Route("~/api/user/login")]
public HttpResponseMessage Login([FromBody]User Source)
{
try
{
User requestedUser = db.Users.Include("Role").FirstOrDefault(x => x.EmailAddress == Source.EmailAddress);
if (requestedUser != null && Hashing.ValidatePassword(Source.Password, requestedUser.Password))
{
return Request.CreateResponse(HttpStatusCode.OK, requestedUser);
}
return Request.CreateResponse(HttpStatusCode.NotFound, "Incorrect username or password");
}
catch (Exception ex)
{
return ReportError(ex, "USER LOGIN");
}
}