[英]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.