Reputation: 1585
I am experimenting with the new Azure Machine Learning Service and have created a web service from my model. The service is working fine, as when I use a HTTPS tool to POST to it, I get the results I would expect.
My issue is with getting my ASP.NET code to work with it. I am using the code provided via the Machine Learning Web Services details page. I know it POSTs everything correctly, and the web service returns the correct JSON as I am packet tracing the communication. 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
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; }
}
}
Upvotes: 1
Views: 1030
Reputation: 1585
This issue is due to a bug which is discussed here - HttpClient.GetAsync(...) never returns when using await/async
Basically you need to add the ConfigureAwait(false) method.
So the line of code now looks like
HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest).ConfigureAwait(false);
Upvotes: 1