Calling async methods from non-async code

后端 未结 4 1405
青春惊慌失措
青春惊慌失措 2020-11-28 19:25

I\'m in the process of updating a library that has an API surface that was built in .NET 3.5. As a result, all methods are synchronous. I can\'t change the API (i.e., conver

4条回答
  •  我在风中等你
    2020-11-28 20:03

    You Can Call Async Method From non-async method .Check below Code .

         public ActionResult Test()
         {
            TestClass result = Task.Run(async () => await GetNumbers()).GetAwaiter().GetResult();
            return PartialView(result);
         }
    
        public async Task GetNumbers()
        {
            TestClass obj = new TestClass();
            HttpResponseMessage response = await APICallHelper.GetData(Functions.API_Call_Url.GetCommonNumbers);
            if (response.IsSuccessStatusCode)
            {
                var result = response.Content.ReadAsStringAsync().Result;
                obj = JsonConvert.DeserializeObject(result);
            }
            return obj;
        }
    

提交回复
热议问题