In this post, I will work on the machine learning predictive model and how to explore the types of client and server applications that you can use to consume Azure Machine Learning web service.

I will walk through building various client and server applications meant to consume Azure Machine Learning predictive models exposed as web services.

This example contains a very simple and basic client application and explores a full-blown ASP.NET web API web service implementation that implements the REST protocol and provides Azure Machine Learning predictions using JSON for the inputs and the outputs.

Let’s begin by looking at the default sample code the call an Azure Machine Learning web services that we have exposed.

You can reach a code sample page by navigation to your Azure Machine Learning workspace from the global menu.

The help page for the Azure Machine Learning API web service provides specific implementation details about the following aspects of making an Azure Machine Learning web service via an HTTP POST request,

Let’s start with creating a new clients application to call our new Azure Machine Learning web service from C#.

Create a Console Application.

Azure Machine Learning

Invoke the NuGet Package Manager console.
Azure Machine Learning

Install the Microsoft.AspNet.WebApi.Client NuGet packages and their dependencies:

Install-Package Microsoft.AspNet.WebApi.Client

Azure Machine Learning

Implementation
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8. First create a DataTable class
  9. public class DataTable
  10. {
  11. public string[] ColumnsNames { get; set; }
  12. public string[,] Values { get; set; }
  13. }

This class contains two string arrays used for invoking the web service.

Then, create in the program class.

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. invokeRequestResponseService().Wait();
  6. }
  7. }

Then, implement the invokeRequestResponseService method.

  1. static async Task invokeRequestResponseService()
  2. {
  3. using (var client =new HttpClient())
  4. {
  5. var score = new
  6. {
  7. Inputs = new Dictionary<string, DataTable>()
  8. {
  9. {
  10. "Input1",
  11. new DataTable()
  12. {
  13. ColumnsNames=new string[] {"Age", "education", "origin"},
  14. Values= new string[,] { {"0","28", "0","value", "0","1" }}
  15. }
  16. },
  17. },
  18. GlobalParameters=new Dictionary<string, string>() { }
  19. };
  20. const string apiKey = "abc123";
  21. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("scheme", apiKey);
  22. client.BaseAddress = new Uri("YOUR URI");
  23. HttpResponseMessage resp = await client.PostAsJsonAsync("", score);
  24. if (resp.IsSuccessStatusCode)
  25. {
  26. string result = await resp.Content.ReadAsStringAsync();
  27. }
  28. else
  29. {
  30. Console.WriteLine("Error {0}", resp.StatusCode);
  31. }
  32. }
  33. }

You can find your API key for your web service by navigating to the web service tab of your Azure Machine Learning Studio workspace.

Azure Machine Learning

The result of the execution.

Azure Machine Learning

In this example, the response back from our call to the Azure Machine Learning web service indicates that this input candidate likely makes less than $50,000 a year. The corresponding confidence level is 0.0010072038276121.