不处理 Azure ASP.NET REST Web Service 调用

本文关键字:Web Service 调用 REST NET 处理 Azure ASP | 更新日期: 2023-09-27 18:33:02

我正在试验新的Azure机器学习服务,并从我的模型创建了一个Web服务。该服务运行良好,因为当我使用 HTTPS 工具向其开机时,我得到了我期望的结果。

我的问题是让我的 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; }
    }
}

不处理 Azure ASP.NET REST Web Service 调用

此问题是由于此处讨论的错误 - HttpClient.GetAsync(...) 在使用 await/async 时永远不会返回

基本上你需要添加 ConfigureAwait(false) 方法。

所以代码行现在看起来像

HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest).ConfigureAwait(false);