我正在并行调用一个较慢的 Web 服务。一切都很好,直到我意识到需要从服务中获取一些信息。但是我不知道在哪里获取这些值。我无法写入数据库,因为使用 Parallel.ForEach 调用的方法内 HttpContext.Current 似乎为空。
以下是一个示例程序(请想象一个较慢的 Web 服务而不是字符串连接)。
以下是一个示例程序(请想象一个较慢的 Web 服务而不是字符串连接)。
using System;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
WordMaker m = new WordMaker();
m.MakeIt();
}
public class WordMaker
{
public void MakeIt()
{
string[] words = { "ack", "ook" };
ParallelLoopResult result = Parallel.ForEach(words, word => AddB(word));
Console.WriteLine("Where did my results go?");
Console.ReadKey();
}
public string AddB(string word)
{
return "b" + word;
}
}
}
Parallel.ForEach
的不同重载可能是您想要的:http://msdn.microsoft.com/zh-cn/library/dd991486.aspx - Austin SalonenFunc<...>
而不是Action<...>
。 - Austin Salonen