我从Microsoft Azure指定的代码中获取了多个错误,用于其机器学习的c#实现。 似乎无法弄清楚错误
我已经安装了Nuget包Microsoft.AspNet.WebApi.Client
。我使用Install-Package Microsoft.AspNet.WebApi.Client
命令时收到错误。所以我用Install-Package Microsoft.AspNet.WebApi -Version 4.0.30506
来安装早期版本。
namespace CallRequestResponseService
{
public class StringTable
{
public string[] ColumnNames { get; set; }
public string[,] Values { get; set; }
}
class Program
{
static void Main(string[] args)
{
InvokeRequestResponseService().Wait();
}
static async Task InvokeRequestResponseService()
{
using (var client = new HttpClient())
{
var scoreRequest = new
{
Inputs = new Dictionary<string, StringTable> ()
{
{
"input1",
new StringTable()
{
ColumnNames = new string[] {"name", "age"},
Values = new string[,] { { "value", "0" }, { "value", "0" } }
}
}
},
GlobalParameters = new Dictionary<string, string>() { }
};
const string apiKey = "abc123"; // Replace this with the API key for the web service
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", apiKey);
client.BaseAddress = new Uri("https://ussouthcentral.services.azureml.net/workspaces/095906590a604077a98678e5d7fc69a0/services/b875062b548b41b884a490724c3cb71c/execute?api-version=2.0&details=true");
// WARNING: The 'await' statement below can result in a deadlock if you are calling this code from the UI thread of an ASP.Net application.
// One way to address this would be to call ConfigureAwait(false) so that the execution does not attempt to resume on the original context.
// For instance, replace code such as:
// result = await DoSomeTask()
// with the following:
// result = await DoSomeTask().ConfigureAwait(false)
HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
Console.WriteLine("Result: {0}", result);
}
else
{
Console.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode));
// Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
Console.WriteLine(response.Headers.ToString());
string responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseContent);
}
}
}
}
}