Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么web服务任务被取消了?我如何找到答案?_C#_.net_Asp.net Web Api2 - Fatal编程技术网

C# 为什么web服务任务被取消了?我如何找到答案?

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

当我尝试使用我编写的在线web服务登录到我的网站时,我根本看不到任何事情发生

我应该指出,当我从localhost运行所有东西时,这是100%有效的

在我的登录中:

    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");
        }
    }