0

当我正在收听大量 POST 请求时,我正在尝试使用类似 Promise 的方法来异步执行它。

问题是它需要“getDataLead”任务之外的返回值(这种情况下取​​消注释return "good2“部分)。

有什么想法可以让 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";

    }

谢谢

4

1 回答 1

0
[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;

    }

这应该迫使您的应用程序从异步内容中获取价值。

于 2018-11-22T15:13:41.090 回答