async POST 方法 C# '': 并非所有代码路径 return 一个值
async POST method C# '': not all code paths return a value
因为我正在收听很多 POST 请求,所以我正在尝试使用类似于 Promise 的方法进行异步处理。
问题是它需要 "getDataLead" 任务之外的 return 值(在这种情况下取消注释 return "good2
" 部分)。
有什么想法可以让 POST 方法等待并 return 异步 "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";
}
谢谢
[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;
}
这应该会强制您的应用程序从异步内容中获取值。
因为我正在收听很多 POST 请求,所以我正在尝试使用类似于 Promise 的方法进行异步处理。
问题是它需要 "getDataLead" 任务之外的 return 值(在这种情况下取消注释 return "good2
" 部分)。
有什么想法可以让 POST 方法等待并 return 异步 "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";
}
谢谢
[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;
}
这应该会强制您的应用程序从异步内容中获取值。