1

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, which i can examine the returned json data

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

JGreasley
  • 301
  • 4
  • 12
  • Have you tried to debug your code? – Alexei Levenkov Oct 20 '14 at 16:04
  • Yes of course, but it's not returning an error. It just sits there after calling Runasync() until the ajax call times out. Like I mentioned above, while working in Fiddler i can observe the call, and the returned data from the call. It just seems not to do anything with the returned data. – JGreasley Oct 20 '14 at 16:12
  • @JGreasley try hitting your http://api.steampowered.com/I... URL in a browser (with all the querystring parameters you're using) and see if it returns anything, maybe its just a bad request. – fooser Oct 20 '14 at 16:14
  • @fooser, done that. As mentioned, i can SEE the returned json data from the API call in fiddler. – JGreasley Oct 20 '14 at 16:16
  • I suspect it getting confused about synchronization context - what happens if you make `RunAsync` static and remove that strange creation of `new _default()`? – Alexei Levenkov Oct 20 '14 at 16:40
  • Removed my answer as it seems less than helpful. I suggest adding a catch for AggregateException and place a breakpoint in there incase it gets hit. I believe GetStringAsync is erroring but you aren't seeing it. – fooser Oct 20 '14 at 16:54
  • :) That post got "junky" so I readded the WebClient piece only. Thanks to @AlexeiLevenkov for some wisdom around running asynchronous code synchronously. – fooser Oct 20 '14 at 17:03

1 Answers1

0

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"));
    }
}
fooser
  • 822
  • 6
  • 14