【发布时间】:2016-07-05 08:46:49
【问题描述】:
我们不得不从以我们无法控制的方式提供给我们的极其有限的 Web API 调用数据。逻辑流程如下:
For each product represented by a list of ID values
Get each batch of sub-categories of type FOO (100 records max. per call)
Keep calling the above until no records remain
Get each batch of sub-categories of type BAR (100 records max. per call)
Keep calling the above until no records remain
目前这会产生近 100 个 Web API 调用(我们已经询问了提供商,没有改进 Web API 来缓解这种情况)。
我担心性能会因此而受到严重影响,因此我试图了解异步替代方案,希望这会有所帮助。一个主要问题是数据只能调用一次。之后,它会被锁定并且不会重新发送,这是我们测试的主要限制。
我已阅读 here、here 和 here,但我正在努力适应我的代码,因为我认为我需要两个 await 调用而不是一个,并且担心事情会搞砸。
任何人都可以将等待和异步逻辑应用到这个伪代码中,以便我可以阅读并尝试遵循正在发生的事情的流程吗?
public class DefaultController : Controller
{
public ActionResult Index()
{
var idlist = new List<String>() {"123", "massive list of strings....", "789"};
var xdoc = new XDocument();
xdoc.Declaration = new XDeclaration("1.0", Encoding.Unicode.WebName, "yes");
var xroot = new XElement("records");
xdoc.Add(xroot);
foreach (string id in idlist)
{
// Get types FOO -----------------------------------
Boolean keepGoingFOO = true;
while (keepGoingFOO)
{
// 100 records max per call
var w = new WebServiceClient();
request.enumType = enumType.FOO;
var response = w.response();
foreach (ResultItem cr in response.ResultList)
{
var xe = new XElement("r");
// create XML
xroot.Add(xe);
}
keepGoingFOO = response.moreRecordsExist;
}
// Get types BAR -----------------------------------
Boolean keepGoingBAR = true;
while (keepGoingBAR)
{
// 100 records max per call
var w = new WebServiceClient();
request.enumType = enumType.BAR;
var response = w.response();
foreach (ResultItem cr in response.ResultList)
{
var xe = new XElement("r");
// create XML
xroot.Add(xe);
}
keepGoingBAR = response.moreRecordsExist;
}
}
return View(xdoc);
}
}
【问题讨论】:
标签: c# .net asp.net-mvc asynchronous async-await