Introduction To Azure ML - Creating An Azure ML Web Service And A Client Application - Part Two

In the first part of this article, we created a web service using Azure ML Studio, and now we are going to develop a client application to invoke it. If you didn't go through the first part yet, please visit the below link.

Evaluate Sample Code 

  • Clicking the Request/Response link under API Help Page will open the API documentation page.

    Azure

  • Scroll down and view the Request Headers and Request Body to get an idea of how the request will be formed. This request is what will be sent to the web service for evaluation and is in JSON format.

  • Scroll down further and evaluate the Response Headers and Response Body sample. This shows the format of data that will be returned in the response as well as a sample of the JSON that will be returned. You can use this sample to understand how to parse the JSON for the data you want to use in your application.

  • Scrolling down further presents information about the input and output parameters which includes the names and datatypes expected. If you will be creating a class to represent the returned information, you can use these parameters to create the proper data types for your member variables.

  • Scroll all the way to the bottom of the web service API documentation window. Note the Sample Code entry contains samples in C#, Python, and R.Select the language that you are comfortable with ( We use C# in this demo)
    Azure

  • Note the location where the API key will need to be replaced. The value abc123 is used as a placeholder and you will paste your API key in this location when creating your client application.

Creating C# Console Application

  • Start Visual Studio.Open a new project .Select VIsual C# console application template.Give a proper name for the project and click ok

  • Visual Studio will then open an application with application stub code.

  • Switch back to Azure API documentation page copy the sample code

  • Replace the default code in the visual studio with the code that we had copied

  • Note the red squiggly line under Formatting in the System.Net.Http.Formatting entry. This means that we are missing a library that implements the functionality necessary for this using statement. Copy the last comment line in the comments at the top of the code file (// Install-Package Microsoft.AspNet.WebApi.Client) and install the library using the nuget package manager console.(Tools-->Nuget package manager-->Package manager console).Verify is there are any more errors in the code.

    Azure

  • In order for our application to access the web service you need to add the api key to our source code. Go to the azure ML studio and copy the api key.

    Azure

  • Locate the const string apiKey entry and replace the abc123 with the copied API key. Be sure to leave the double quotes surrounding the key.
    1. const string apiKey = "abc123"// Replace this with the API key for the web service  
    2. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);  

  • Next, locate the Values = new string[,] line of code. Replace the first set of values with the same sample data that we used above for a back check..
    1. Inputs = new Dictionary < string, StringTable > () {  
    2.     {  
    3.         "input1",  
    4.         new StringTable() {  
    5.             ColumnNames = new string[] {  
    6.                     "WallArea",  
    7.                     "RoofArea",  
    8.                     "OverallHeight",  
    9.                     "GlazingArea",  
    10.                     "HeatingLoad"  
    11.                 },  
    12.                 Values = new string[, ] {  
    13.                     {  
    14.                         "250",  
    15.                         "110.25",  
    16.                         "8",  
    17.                         "0",  
    18.                         "14.55"  
    19.                     }  
    20.                 }  
    21.         }  
    22.     },  
    23. },  

  • Run your application and verify the output.

    Azure

Next Recommended Readings