private async Task <TextClassificationResultItem> GetItemCResult( TextClassificationModel model) { IConfigurationBuilder builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); string url = builder.Build().GetValue <string>( "CognitionService:TextClassification:url"); string vendorC_AppId = builder.Build().GetValue <string>( "CognitionService:TextClassification:VendorC_AppId"); string vendorC_SecretKey = builder.Build().GetValue <string>( "CognitionService:TextClassification:VendorC_SecretKey"); string vendor = builder.Build().GetValue <string>( "CognitionService:TextClassification:VendorC_CognitionServiceVendor"); //string vendorA_AppId = builder.Build().GetValue<string>( // "CognitionService:TextClassification:VendorB_AppId"); //string vendorA_SecretKey = builder.Build().GetValue<string>( // "CognitionService:TextClassification:VendorB_SecretKey"); var client = new HttpClient { BaseAddress = new Uri(url), }; client.DefaultRequestHeaders.Add("appId", vendorC_AppId); client.DefaultRequestHeaders.Add("appSecret", vendorC_SecretKey); FormUrlEncodedContent content = new FormUrlEncodedContent( new Dictionary <string, string> { { "title", model.Title }, { "text", model.Content } }); HttpResponseMessage msg = await client.PostAsync(url, content); string result = await msg.Content.ReadAsStringAsync(); TextClassificationResultItem item = new TextClassificationResultItem() { VendorLabel = "C", CognitionServiceVendor = vendor, VendorResult = result, }; return(item); }
public IActionResult AddNew(TextClassificationModel model) { if (model != null && ModelState.IsValid) { TextClassificationRepository repository = new TextClassificationRepository(); List <TextClassificationResultItem> resultItems = new List <TextClassificationResultItem>(); Task task1 = Task.Run(() => { try { Task <TextClassificationResultItem> taskA = this.GetItemAResult(model); taskA.Wait(); TextClassificationResultItem itemA = taskA.Result; if (itemA != null) { resultItems.Add(itemA); } } catch (Exception ex1) { System.Diagnostics.Trace.WriteLine("GetA: " + ex1.Message + "," + ex1.StackTrace); } }); Task task2 = Task.Run(() => { try { Task <TextClassificationResultItem> taskB = this.GetItemBResult(model); taskB.Wait(); TextClassificationResultItem itemB = taskB.Result; if (itemB != null) { resultItems.Add(itemB); } } catch (Exception ex1) { System.Diagnostics.Trace.WriteLine("GetB: " + ex1.Message + "," + ex1.StackTrace); } }); Task task3 = Task.Run(() => { try { Task <TextClassificationResultItem> taskC = this.GetItemCResult(model); taskC.Wait(); TextClassificationResultItem itemC = taskC.Result; if (itemC != null) { resultItems.Add(itemC); } } catch (Exception ex1) { System.Diagnostics.Trace.WriteLine("GetC: " + ex1.Message + "," + ex1.StackTrace); } }); Task.WaitAll(task1, task2, task3); TextClassificationResult newResult = new TextClassificationResult() { Ctime = DateTime.Now, Title = model.Title, Content = model.Content, ResultItems = resultItems }; repository.Add(newResult); return(RedirectToAction("TextClassification", "Home")); } return(View("AddNew", model)); }