[英]Not Processing Azure ASP.NET REST Web Service Call
我正在試驗新的Azure機器學習服務,並已根據我的模型創建了一個Web服務。 該服務運行良好,因為當我使用HTTPS工具對其進行POST時,可以獲得預期的結果。
我的問題是讓我的ASP.NET代碼可以使用它。 我正在使用通過機器學習Web服務詳細信息頁面提供的代碼。 我知道它可以正確地發布所有內容,並且在我跟蹤通信時,Web服務返回正確的JSON。 但是由於某種原因,我的代碼無法確認此返回。
我已經從Azure網站和Visual Studio中的本地站點運行了此代碼
namespace website
{
public partial class ML : Page
{
protected void Page_Load(object sender, EventArgs e)
{
InvokeRequestResponseService().Wait();
//await InvokeRequestResponseService();
}
static async Task InvokeRequestResponseService()
{
using (var client = new HttpClient())
{
ScoreData scoreData = new ScoreData()
{
FeatureVector = new Dictionary<string, string>()
{
{ "age", "0" },
{ "education", "0" },
{ "education-num", "0" },
{ "marital-status", "0" },
{ "relationship", "0" },
{ "race", "0" },
{ "sex", "0" },
{ "capital-gain", "0" },
{ "capital-loss", "0" },
{ "hours-per-week", "0" },
{ "native-country", "0" },
},
GlobalParameters =
new Dictionary<string, string>()
{
}
};
ScoreRequest scoreRequest = new ScoreRequest()
{
Id = "score00001",
Instance = scoreData
};
const string apiKey = "dg/pwCd7zMPc57lfOSJqxP8nbtKGV7//XXXXXXXXXXXXXXXXXXXXXXXXXXXXgvdVl/7VWjqe/ixOA=="; // Replace this with the API key for the web service
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
client.BaseAddress = new Uri("https://ussouthcentral.services.XXXXXXX.net/workspaces/a932e11XXXXXXXXXXX29a69170eae9ed4/services/e8796c4382fb4XXXXXXXXXXXddac357/score");
// GETS STUCK ON THE NEXT LINE
HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest); <---- NEVER RETURNS FROM THIS CALL
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
Console.WriteLine("Result: {0}", result);
}
else
{
Console.WriteLine("Failed with status code: {0}", response.StatusCode);
}
}
}
}
public class ScoreData
{
public Dictionary<string, string> FeatureVector { get; set; }
public Dictionary<string, string> GlobalParameters { get; set; }
}
public class ScoreRequest
{
public string Id { get; set; }
public ScoreData Instance { get; set; }
}
}
此問題是由於這里討論的錯誤所致- 使用await / async時HttpClient.GetAsync(...)從不返回
基本上,您需要添加ConfigureAwait(false)方法。
所以代碼行現在看起來像
HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest).ConfigureAwait(false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.