private async void InvokeRequestResponseService()

in Solutions/HeartDisease/HeartDisease/HeartDiseasePrediction - CS/MainPage.xaml.cs [57:132]


        private async void InvokeRequestResponseService()
        {
            ring.IsActive = true;

            try
            {
                using (var client = new HttpClient())
                {
                    var scoreRequest = new
                    {

                        Inputs = new Dictionary<string, StringTable>() {
                        {
                            "input1",
                            new StringTable()
                            {
                                ColumnNames = new string[] { "heart_disease_diag", "thal", "chestpaintype", "number_of_major_vessel",
                                    "st_depression_induced_by_exercise", "exercise_induced_angina", "max_heart_rate",
                                    "slope_of_peak_exercise", "age"},
                                Values = new string[,] {  { "0",
                                                            comboBoxThal.SelectedValue.ToString().Substring(0, 1),
                                                            comboBoxCp.SelectedValue.ToString().Substring(0, 1),
                                                            comboBoxCa.SelectedValue.ToString().Substring(0, 1),
                                                            textBoxOldpeak.Text,
                                                            comboBoxExang.SelectedValue.ToString().Substring(0, 1),
                                                            textBoxThalach.Text,
                                                            comboBoxSlope.SelectedValue.ToString().Substring(0, 1),
                                                            textBoxAge.Text } }

                            }
                        },
                                        },
                        GlobalParameters = new Dictionary<string, string>()
                        {
                        }
                    };

                    const string apiKey = "BqLG47rjT/ox07z4UiSzQT6YNX1k0FUXIfQyqLGKTLyRStzdf9wrmxd5qGgmvlglcEIObmjR1w9rohj6vsJTzA=="; // 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/3deea62efa414e73b85abc9652f52010/services/18454b9e6a75450299ed56450d99a881/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);

                    ring.IsActive = false;

                    if (response.IsSuccessStatusCode)
                    {
                        string result = await response.Content.ReadAsStringAsync();

                        JsonObject jsonObject = JsonValue.Parse(result).GetObject().GetNamedObject("Results").GetNamedObject("output1").GetNamedObject("value");
                        var responseBody = JsonConvert.DeserializeObject<StringTable>(jsonObject.ToString());

                        var percentage = float.Parse(responseBody.Values[0, 10]) * 100;
                        textBlockResult.Text = String.Format("Probability of Heart Disease is {0:0.00}%", percentage);
                        icon.Text = (percentage > 50) ? "😧" : "😃";
                    }
                    else
                    {
                        textBlockResult.Text = String.Format("The request failed: {0}", response.StatusCode);
                    }
                }
            }
            catch (Exception)
            {
                ring.IsActive = false;
                textBlockResult.Text = "Parameter incorrect";
            }
        }