[英]Not Processing Azure ASP.NET REST Web Service Call
I am experimenting with the new Azure Machine Learning Service and have created a web service from my model. 我正在试验新的Azure机器学习服务,并已根据我的模型创建了一个Web服务。 The service is working fine, as when I use a HTTPS tool to POST to it, I get the results I would expect. 该服务运行良好,因为当我使用HTTPS工具对其进行POST时,可以获得预期的结果。
My issue is with getting my ASP.NET code to work with it. 我的问题是让我的ASP.NET代码可以使用它。 I am using the code provided via the Machine Learning Web Services details page. 我正在使用通过机器学习Web服务详细信息页面提供的代码。 I know it POSTs everything correctly, and the web service returns the correct JSON as I am packet tracing the communication. 我知道它可以正确地发布所有内容,并且在我跟踪通信时,Web服务返回正确的JSON。 But for some reason my code does not acknowledge this return. 但是由于某种原因,我的代码无法确认此返回。
I have run this code from both an Azure Website an a local site within Visual Studio 我已经从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; }
}
}
This issue is due to a bug which is discussed here - HttpClient.GetAsync(...) never returns when using await/async 此问题是由于这里讨论的错误所致- 使用await / async时HttpClient.GetAsync(...)从不返回
Basically you need to add the ConfigureAwait(false) method. 基本上,您需要添加ConfigureAwait(false)方法。
So the line of code now looks like 所以代码行现在看起来像
HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest).ConfigureAwait(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.