/// <summary> /// POST: api/Messages /// Receive a message from a user and reply to it /// </summary> public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { if (activity.Type == ActivityTypes.Message) { ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); // 만약 JSON 데이터일 경우 Azure ML로 Iris Prediction 요청 수행 if (ValidateJSON(activity.Text)) { // Azure ML로 JSON을 요청 Iris iris = JsonConvert.DeserializeObject <Iris>(activity.Text); // Azure ML을 호출하는 함수 InvokeRequestResponseService(iris).Wait(); Activity reply = activity.CreateReply("예측 결과 : " + iris.PredictVal); await connector.Conversations.ReplyToActivityAsync(reply); } else { // 일반 텍스트. echo 수행 // calculate something for us to return int length = (activity.Text ?? string.Empty).Length; // return our reply to the user Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters"); await connector.Conversations.ReplyToActivityAsync(reply); } } else { HandleSystemMessage(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }
public static async Task InvokeRequestResponseService(Iris iris) { using (var client = new HttpClient()) { var scoreRequest = new { Inputs = new Dictionary <string, StringTable>() { { "input1", new StringTable() { // Azure ML에 만들어둔 Predict Model의 Request param. ColumnNames = new string[] { "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width", "Species" }, Values = new string[, ] { { iris.SepalLength, iris.SepalWidth, iris.PetalLength, iris.PetalWidth, "value" }, } } }, }, GlobalParameters = new Dictionary <string, string>() { } }; const string apiKey = "<키값으로 변경>"; // Azure ML 접근을 위한 키값 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); client.BaseAddress = new Uri("https://asiasoutheast.services.azureml.net/<AzureMLURL>"); // 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).ConfigureAwait(false); if (response.IsSuccessStatusCode) { string result = await response.Content.ReadAsStringAsync(); Debug.WriteLine("Result: {0}", result); iris.PredictVal = result; } else { Debug.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 Debug.WriteLine(response.Headers.ToString()); string responseContent = await response.Content.ReadAsStringAsync(); Debug.WriteLine(responseContent); } } }