简体   繁体   中英

Calling an Async Method, from within an Web Method and getting a return

I'm having difficulties moving a piece of working code, into a web method. I'm playing around with the SteamAPI, and the async method RunAsync(), it was all previously working when it was all handled in a codebehind.

But I wanted to move this action into a Web Method, handled by JQuery .AJAX(). I'm basically calling the method from within the web method and hoping to get the data back to be handled/represented by JQuery. I dealt with many Web Methods before, but none calling non-static methods and async methods.

I'm not actually getting an error, but when calling the API, it just sits there, i can see it requests the data in fiddler (and returns it), but it never moves on from that point, as if it hasn't recieved the 'move on i have my data' command. Eventually my .ajax call will tiumeout after 30secs.

Can anyone see why? I place certain break points, but it never moves on from

string res = await client.GetStringAsync("IPlayerService/GetOwnedGames/v0001/?key=my_steam_key&steamid=my_steam_id&include_appinfo=1&include_played_free_games=1&format=json");

even after fiddler shows there has been a response.

Please see code, and screenshots.

    [System.Web.Services.WebMethod]
    public static async Task<string> Find_Games(string user)
    {
        string rVal = string.Empty;
        dynamic user_game_data;

        try
        {
            var thisPage = new _default();
            user_game_data = await thisPage.RunAsync();

            return user_game_data;
        }
        catch (Exception err)
        {
            throw new Exception(err.Message);
        }

    }


    public async Task<string> RunAsync()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://api.steampowered.com/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            //client.Timeout = System.TimeSpan.FromMilliseconds(15000); //15 Secs

            try
            {
                string res = await client.GetStringAsync("IPlayerService/GetOwnedGames/v0001/?key=my_steam_key&steamid=my_steam_id&include_appinfo=1&include_played_free_games=1&format=json");

                // Read & Deserialize data
                //dynamic json_data = JsonConvert.DeserializeObject(res);
                ////int gamecount = json_data.response.game_count;
                //await saveOwnedGames(json_data, gamecount);
                return res;
            }
            catch (HttpRequestException e)
            {
                throw new Exception(e.Message);
            }

        }
    }

Fiddler Response,我可以检查返回的json数据

Thanks in advance, and let me know if you need any more information.

You may be able to accomplish this without the async stuff.

Give WebClient a try:

[System.Web.Services.WebMethod]
public static string Find_Games(string user)
{
    using (var client = new System.Net.WebClient())
    {
        return client.DownloadString(String.Concat("http://api.steampowered.com/", "IPlayerService/GetOwnedGames/v0001/?key=my_steam_key&steamid=my_steam_id&include_appinfo=1&include_played_free_games=1&format=json"));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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