简体   繁体   中英

Asp.net Azure machine learning

Im currently doing a prediction project and was wondering if any knew how to connect the machine learning sample code with asp.net ? I quite new to asp.net to experience with c# Here is the sample code:

// This code requires the Nuget package Microsoft.AspNet.WebApi.Client to be installed.
// Instructions for doing this in Visual Studio:
// Tools -> Nuget Package Manager -> Package Manager Console
// Install-Package Microsoft.AspNet.WebApi.Client

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

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[] { "Number1", "Number2", "Number3", "Number4", "Number5", "Number6" },
                                Values = new string[,] {  { "0", "0", "0", "0", "0", "0" },  { "0", "0", "0", "0", "0", "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/08ebbc1613d5478285ec11b4516223d4/services/b1fcf5664b1b4b188b1dda6de194c91e/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);
                }
            }
        }
    }
}

I have earlier created a tutorial on how to do a simple prediction (super simple example on how to predict the income based on Gender and Age).

It is in Norwegian, but it is fairly simple to understand what I am doing, so just follow along: https://channel9.msdn.com/Series/MSDEVNO/Lr-Azure-Machine-Learning-p-1-2-3

The dataset is in the description (so you can copy that).

I have also created a Github repo that you can clone (or download zip): https://github.com/readyforchaos/howmuchmonneh

You would want to download the " HowMuchMoney solution " and take a look at how I send a request (post) to the API based on one age and gender-input, then how I read (parse) the JSON response (GET) from the API that it gives back (the response).

The [HttpPost] is in the "Controllers/HomeController.cs"

And to make things cleaner, I have put the API key in the "HowMuchMonneh/WebService/IncomeWebService.cs" file.

Follow above instructions and do these-

Replace

HttpResponseMessage response = client.PostAsJsonAsync("",scoreRequest).Result;

with

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

and copy this to matching line

string result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM