This async method lacks 'await' operators and will run synchronously RRS feed

  • Question

  • I am getting this warning This async method lacks 'await' operators and will run synchronously

    How to get rid of this warning ? where to use await keyword in my function body ? this is public async Task<ResponseContext> GetDataFromAPI(string tickername) the signature of the function where i need to use await keyword but i am not being able to use it. so guide me

    This is my function code

    public async Task<ResponseContext> GetDataFromAPI(string tickername)
                string result = "";
                //url will contain api url
                string url = "";
                //oResponseContext will store return data along with status & error details if fails
                ResponseContext oResponseContext = new ResponseContext();
                //allXfundData will store all 8 form data from url
                List<XfundData> allXfundData = new List<XfundData>();
                //WebClient will call the api by url
                WebClient client = null;
                //data will hold incoming data in stream format
                Stream data = null;
                //StreamReader will contain the stream of data
                StreamReader reader = null;
                //csv data will be deserialize to dataresult variable for each iteration
                List<XfundData> dataresult = null;
                //data will be fetch by 8 iteration & will store in alldata
                List<string> alldata = null;
                    //iterate in 8 time to get data form no wise 
                    //the form no passing also in url
                    for (int x = 0; x < 8; x++)
                        url = string.Format("{0}{1}{2}{3}", "", tickername, "&Form=", x.ToString());
                        //fetching data through WebClient
                        client = new WebClient();
                        data = client.OpenRead(url);
                        reader = new StreamReader(data);
                        //reading data and storing into string variable result
                        result = reader.ReadToEnd();
                        alldata = new List<string>();
                        //data is in CSV format so split line by new line
                        if (!String.IsNullOrEmpty(result) && result.Length > 12 && result.Contains(","))
                            alldata = result.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();
                        dataresult = new List<XfundData>();
                        if (alldata.Any())
                            //parse CSV data by LINQ and result will be stored in alldata
                            dataresult = (from datas in alldata
                                          where datas.Split(',').ToArray().Count() > 1
                                          select new XfundData
                                              Stand = datas.Split(',')[1],
                                              StandardName = datas.Split(',')[2],
                                              AsCode = datas.Split(',')[3],
                                              AsReportedItemName = datas.Split(',')[4],
                                              Scale = datas.Split(',')[5]
                        if (dataresult.Any())
                            //finally data will be added to allXfundData for each iteration
                    //storing data & status in ResponseContext class when success
                    oResponseContext.IsSuccess = true;
                    oResponseContext.Message = "Success";
                    oResponseContext.ResponseData = allXfundData;
                catch(Exception ex)
                    //storing exception & message in ResponseContext class when fail
                    oResponseContext.IsSuccess = false;
                    oResponseContext.Message = "Error "+ex.Message.ToString();
                    oResponseContext.ResponseData = null;
                    //disposing final list
                    allXfundData = null;
                //return result in the form of ResponseContext
                return oResponseContext;

    XFundMapper oTickerList = new XFundMapper();
    var task1 = oTickerList.GetDataFromAPI(cboTicker.Text);
    await Task.WhenAll(task1);
    ResponseContext oResponseContext = task1.Result;
    	dgSourceFundamental.DataSource = oResponseContext.ResponseData;

    Monday, September 9, 2019 9:17 AM


  • If you mark a method as async then it must have at least 1 await in it. If it doesn't then don't use async.

    Each time you call a Task-returning method you should use await before it. None of the code you posted is task-based so you don't need async. Furthermore your method does not benefit from returning Task<T> because it runs completely sychronously. Just return your ResponseContext directly and your warnings go away.

    If you want/need this code to be async then you need to take your function apart, identify each section that can be run async and convert it to an async function. Then replace the code in this function with an awaited call to the new function you created. If you cannot do that because of how the code works then you cannot make this an async method. The only place in the code that I see an option to use async is in your for loop's use of WebClient.OpenRead which has an async version. However you aren't going to gain a lot unless you convert more of the code to use async by breaking your function up into smaller pieces. You are violating the SRP here and that is just bad functional programming. Fix the functional programming issue first, then add async to optimize it, don't do it the other way around.

    Michael Taylor

    • Marked as answer by Sudip_inn Monday, September 9, 2019 7:22 PM
    Monday, September 9, 2019 1:58 PM