简体   繁体   English

当我尝试调用Web API时,Windows Phone应用程序无法正常运行

[英]Windows phone application does not work properly when I'm trying to call a web api

I am developing a simple windows phone 8 application. 我正在开发一个简单的Windows Phone 8应用程序。
I do not use MVVM , it is very simple. 我不使用MVVM ,这非常简单。
I have a button, and when I press it I want to add some data into the database (using the post method). 我有一个按钮,当我按下它时,我想向数据库中添加一些数据(使用post方法)。
The web API is made in asp.net and it works properly because I tested with fiddler , and also with a console application. Web API是在asp.net制作的,并且可以正常工作,因为我使用了fiddler和控制台应用程序进行了测试。
The code behind from the button is the following: 该按钮后面的代码如下:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        HttpClient client = new HttpClient();
        string baseUrl = "myWebApiLink";
        client.BaseAddress = new Uri(baseUrl);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        string serviceUrl;
        serviceUrl = "api/People";

        var anEmployee = new People()
         {
             FirstName = "Windows",
             LastName = "Phone",
             Age = 8
         };
         HttpResponseMessage response;
         response = client.PostAsJsonAsync(serviceUrl, anEmployee).Result; //the problem
         if (response.IsSuccessStatusCode)
         {
             //some success messages
         }
         else
         {
             //some fail messages
         }
UriKind.Relative));
    }

At the line 在生产线上

response = client.PostAsJsonAsync(serviceUrl, anEmployee).Result;  

the application just gets blocked. 该应用程序只是被阻止。
It does not throw an exception, it just stays on that line. 它不会引发异常,它只会停留在该行上。

Any ideas? 有任何想法吗?

Thank you in advance! 先感谢您!

Actually i dont think it says on that line, you are calling an asynchronous method, which means your code continues executing and does not wait for the response. 实际上,我不认为这句话说的是,您正在调用异步方法,这意味着您的代码将继续执行,并且不等待响应。 Changing the method signature by adding async, and then awaiting the respoonse from the call will work. 通过添加异步来更改方法签名,然后等待调用重新启动将起作用。 Here is your code modified. 这是修改您的代码。

private async void Button_Click(object sender, RoutedEventArgs e)
    {
        HttpClient client = new HttpClient();
        string baseUrl = "myWebApiLink/";
        client.BaseAddress = new Uri(baseUrl);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        string serviceUrl;
        serviceUrl = "api/People";

        var anEmployee = new People()
        {
            FirstName = "Windows",
            LastName = "Phone",
            Age = 8
        };

        var request = new HttpRequestMessage(HttpMethod.Post, serviceUrl);
        var requestContent = anEmployee; //this value is a string, check the format on your server

        request.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");

        HttpResponseMessage response;
        response = await client.SendAsync(request); //no longer the problem

        if (response.IsSuccessStatusCode)
        {
            //some success messages
        }
        else
        {
            //some fail messages
        }
    }

use async button client and await on post async call... here is how. 使用异步按钮客户端并等待异步调用后...这是方法。

private async void Button_Click(object sender, RoutedEventArgs e)
{
    HttpClient client = new HttpClient();
    string baseUrl = "myWebApiLink";
    client.BaseAddress = new Uri(baseUrl);
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    string serviceUrl;
    serviceUrl = "api/People";
    var anEmployee = new People()
     {
         FirstName = "Windows",
         LastName = "Phone",
         Age = 8
     };
    HttpResponseMessage response;
    response = await client.PostAsJsonAsync(serviceUrl, anEmployee).Result;
    if (response.IsSuccessStatusCode)
    {
        //some success messages
    }
    else
    {
        //some fail messages
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM