Search code examples
c#asynchronouspostpromisecontinuewith

async POST method C# '': not all code paths return a value


As I'm listening to lots of POST requests I'm trying to do it asynchronous with Promise like methodology.

The issue is that it it requires a return value outside of the "getDataLead" task(this case to uncomment the return "good2" part).

Any ideas how can I make it so the POST method waits and returns the response from the asynced "matchLogic" function?

   [HttpPost]
    public async Task<string> Post([FromForm]string id)
    {          
        String filterType = "id";             
        string filterValues = id;            

        int batchSize = 50;//max 300, default 300
        String[] fields = { "email", "country", "city", "address", "postalCode", "phone", "company", "billingCountry", "billingCity", "billingPostalCode", "billingStreet", "mainPhone", "website" };//array of field names to retrieve
        String nextPageToken = "";//paging token

        Task<string> tr = await getDataLead(filterType, filterValues, batchSize, fields, nextPageToken).ContinueWith((t1) =>
            {

                if (t1.Exception == null)
                {
                    getLeadsByFilterTypeRootObject data = JsonConvert.DeserializeObject<getLeadsByFilterTypeRootObject>(t1.Result);
                    if (data.success == true)
                    {
                        if (data.result.Count < 2)
                        {
                            return matchLogic(data.result[0]);                            
                        }
                        else
                        {
                            return Task.FromResult("not good");
                        }
                    }
                    else
                    {

                        return Task.FromResult("not good");
                    }
                }
                else
                {
                    return Task.FromResult("not good");
                }  
            });         

        //   return "good2";

    }

Thank you


Solution

  • [HttpPost]
        public async Task<string> Post([FromForm]string id)
        {          
            String filterType = "id";             
            string filterValues = id;
            Task<string> result = string.Empty;            
    
            int batchSize = 50;//max 300, default 300
            String[] fields = { "email", "country", "city", "address", "postalCode", "phone", "company", "billingCountry", "billingCity", "billingPostalCode", "billingStreet", "mainPhone", "website" };//array of field names to retrieve
            String nextPageToken = "";//paging token
    
            Task<string> tr = await getDataLead(filterType, filterValues, batchSize, fields, nextPageToken).ContinueWith((t1) =>
                {
    
                    if (t1.Exception == null)
                    {
                        getLeadsByFilterTypeRootObject data = JsonConvert.DeserializeObject<getLeadsByFilterTypeRootObject>(t1.Result);
                        if (data.success == true)
                        {
                            if (data.result.Count < 2)
                            {
                                result = matchLogic(data.result[0]);                            
                            }
                            else
                            {
                                result = Task.FromResult("not good");
                            }
                        }
                        else
                        {
    
                            result = Task.FromResult("not good");
                        }
                    }
                    else
                    {
                        result = Task.FromResult("not good");
                    }  
                });         
    
              return result;
    
        }
    

    That should force your application to get the value from the async stuff.