简体   繁体   English

异步POST方法C#'':并非所有代码路径都返回值

[英]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. 当我收听大量POST请求时,我正在尝试使用Promise之类的方法进行异步处理。

The issue is that it it requires a return value outside of the "getDataLead" task(this case to uncomment the return "good2 " part). 问题在于,它需要“ getDataLead”任务之外的返回值(这种情况下,取消对return "good2return "good2部分的注释)。

Any ideas how can I make it so the POST method waits and returns the response from the asynced "matchLogic" function? 有什么想法可以使POST方法等待并返回异步“ matchLogic”函数的响应吗?

   [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 谢谢

[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. 那应该迫使您的应用程序从异步内容中获取价值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM