我正在尝试使用控制台应用程序:
我创建了一个使用ContinueWith()方法的任务列表。
foreach (var instance in waveInstances)
{
getTasks.Add(firstService(instance, vendor).ContinueWith(t => secondService(t.Result), TaskContinuationOptions.AttachedToParent));
}
while (getTasks.Count > 0)
{
var firstFinishedTask = await Task.WhenAny(getTasks);
getTasks.Remove(firstFinishedTask);
var instance = await firstFinishedTask;
WriteToDb(instance);
}
我的问题出现在我在ContinueWith()中调用的secondService中。我的代码基本上看起来像:
private async Task<taskData> filterForVendor(string json)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.Accept = "application/json";
request.ContentType = "application/json";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
...Continue to GetResponseString
我使用完全相同的样式,但只在firstService中执行GET。我甚至尝试将该方法完全复制为测试但是在尝试GetRequestStream()时它仍然只是静默地结束,为什么这只发生在ContinueWith()部分?