简体   繁体   English

从Web方法中调用异步方法并获得返回

[英]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. 我在将一部分工作代码移入Web方法时遇到困难。 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. 我正在使用SteamAPI和异步方法RunAsync(),当它们全部在后台代码中处理时,以前都可以使用。

But I wanted to move this action into a Web Method, handled by JQuery .AJAX(). 但是我想将此动作移到由JQuery .AJAX()处理的Web方法中。 I'm basically calling the method from within the web method and hoping to get the data back to be handled/represented by JQuery. 我基本上是从web方法中调用该方法,并希望将数据返回给JQuery处理/表示。 I dealt with many Web Methods before, but none calling non-static methods and async methods. 我以前处理过许多Web方法,但是没有一个调用非静态方法和异步方法。

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. 我实际上并没有收到错误,但是当调用API时,它只是坐在那儿,我可以看到它在提琴手中请求数据(并返回它),但是从那一点开始它就再也没有发生,就好像它没有收到“继续移动我的数据”命令。 Eventually my .ajax call will tiumeout after 30secs. 最终,我的.ajax通话将在30秒后消失。

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: 尝试一下WebClient

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

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

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