· 6 years ago · Oct 19, 2019, 11:50 PM
1// This code requires the Nuget package Microsoft.AspNet.WebApi.Client to be installed.
2// Instructions for doing this in Visual Studio:
3// Tools -> Nuget Package Manager -> Package Manager Console
4// Install-Package Microsoft.AspNet.WebApi.Client
5
6using System;
7using System.Collections.Generic;
8using System.IO;
9using System.Net.Http;
10using System.Net.Http.Formatting;
11using System.Net.Http.Headers;
12using System.Text;
13using System.Threading.Tasks;
14
15namespace CallRequestResponseService
16{
17 class Program
18 {
19 static void Main(string[] args)
20 {
21 InvokeRequestResponseService().Wait();
22 }
23
24 static async Task InvokeRequestResponseService()
25 {
26 using (var client = new HttpClient())
27 {
28 var scoreRequest = new
29 {
30 Inputs = new Dictionary<string, List<Dictionary<string, string>>> () {
31 },
32 GlobalParameters = new Dictionary<string, string>() {
33 {
34 "Database server name", ""
35 },
36 }
37 };
38
39 const string apiKey = "abc123"; // Replace this with the API key for the web service
40 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", apiKey);
41 client.BaseAddress = new Uri("https://ussouthcentral.services.azureml.net/workspaces/1adbe6f815694a149ebcc953d6951b0e/services/6f43475b7fc342a18a9a4d7777f8d1fb/execute?api-version=2.0&format=swagger");
42
43 // WARNING: The 'await' statement below can result in a deadlock
44 // if you are calling this code from the UI thread of an ASP.Net application.
45 // One way to address this would be to call ConfigureAwait(false)
46 // so that the execution does not attempt to resume on the original context.
47 // For instance, replace code such as:
48 // result = await DoSomeTask()
49 // with the following:
50 // result = await DoSomeTask().ConfigureAwait(false)
51
52 HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);
53
54 if (response.IsSuccessStatusCode)
55 {
56 string result = await response.Content.ReadAsStringAsync();
57 Console.WriteLine("Result: {0}", result);
58 }
59 else
60 {
61 Console.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode));
62
63 // Print the headers - they include the requert ID and the timestamp,
64 // which are useful for debugging the failure
65 Console.WriteLine(response.Headers.ToString());
66
67 string responseContent = await response.Content.ReadAsStringAsync();
68 Console.WriteLine(responseContent);
69 }
70 }
71 }
72 }