[英]Calling an Async Method, from within an Web Method and getting a return
我在将一部分工作代码移入Web方法时遇到困难。 我正在使用SteamAPI和异步方法RunAsync(),当它们全部在后台代码中处理时,以前都可以使用。
但是我想将此动作移到由JQuery .AJAX()处理的Web方法中。 我基本上是从web方法中调用该方法,并希望将数据返回给JQuery处理/表示。 我以前处理过许多Web方法,但是没有一个调用非静态方法和异步方法。
我实际上并没有收到错误,但是当调用API时,它只是坐在那儿,我可以看到它在提琴手中请求数据(并返回它),但是从那一点开始它就再也没有发生,就好像它没有收到“继续移动我的数据”命令。 最终,我的.ajax通话将在30秒后消失。
谁能看到原因? 我设置了某些断点,但是从
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");
甚至在提琴手显示出反应之后。
请查看代码和屏幕截图。
[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);
}
}
}
在此先感谢您,如果您需要更多信息,请告诉我。
您可能无需异步即可完成此操作。
尝试一下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.