2012-03-23 44 views
1

需要调用WCF 3个服务如下,我如何使多个并行请求的web服务在C#

var client = new WebClient(); 
    client.Headers[HttpRequestHeader.ContentType] = "application/json"; 
    var resultJson1 = client.UploadString("http://localhost:45868/Product/GetAvailableProductsByContact", 
             jsonser1); 
var resultJson2= client.UploadString("http://localhost:45868/Product/GetMemberProductsByContact", 
             jsonser2); 

var resultJson3= client.UploadString("http://localhost:45868/Product/GetCoachProductsByContact", 
             jsonser3); 

这需要大量的时间来获取页面上的记录和显示。任何人都可以帮助我如何平行执行它们吗?

+0

把这个放入另一个类并多线程吗? – 2012-03-23 18:06:25

回答

5

可以使用task parallelism library

Task<string>[] taskArray = new Task<string>[] 
    { 
     Task.Factory.StartNew(() => { 
      var client = new WebClient(); 
      client.Headers[HttpRequestHeader.ContentType] = "application/json"; 
      var json = client.UploadString("http://localhost:45868/Product/GetAvailableProductsByContact", 
            jsonser1); 
      return json; 
     }), 
     Task.Factory.StartNew(() => { 
      var client = new WebClient(); 
      client.Headers[HttpRequestHeader.ContentType] = "application/json"; 
      var json = client.UploadString("http://localhost:45868/Product/GetMemberProductsByContact", 
            jsonser2); 
      return json; 
     }), 
     Task.Factory.StartNew(() => { 
      var client = new WebClient(); 
      client.Headers[HttpRequestHeader.ContentType] = "application/json"; 
      var json = client.UploadString("http://localhost:45868/Product/GetCoachProductsByContact", 
            jsonser3); 
      return json; 
     }), 
    }; 

// the request for .Result is blocking and waits until each task 
// is completed before continuing; however, they should also all 
// run in parallel instead of sequentially. 
var resultJson1 = taskArray[0].Result; 
var resultJson2 = taskArray[1].Result; 
var resultJson3 = taskArray[2].Result; 

或者,因为你的要求都非常相似,只是由URL和上载的字符串不同,您可以使用LINQ AsParallel阵列处理:

var requests = new [] { 
    new { Url = "http://localhost:45868/Product/GetAvailableProductsByContact", Input = jsonser1 }, 
    new { Url = "http://localhost:45868/Product/GetMemberProductsByContact", Input = jsonser2 }, 
    new { Url = "http://localhost:45868/Product/GetCoachProductsByContact", Input = jsonser3 }, 
}; 
var result = requests.AsParallel().Select(req => { 
    var client = new WebClient(); 
    client.Headers[HttpRequestHeader.ContentType] = "application/json"; 
    var json = client.UploadString(req.Url, req.Input); 
    return json; 
}).ToArray(); 

// when above code has finished running, all tasks are completed 
var resultJson1 = result[0]; 
var resultJson2 = result[1]; 
var resultJson3 = result[2]; 
+0

非常感谢您的回答。这确实会加速这个过程。 – Krishh 2012-03-23 18:25:39

+1

我没有测试任何上面的代码。让我知道它是否会产生编译时或运行时错误。您可能还想熟悉一下'AggregateException',以便在一个或多个请求无法完成时处理错误。 – mellamokb 2012-03-23 18:29:42

0

尝试创建执行您的每个呼叫的TaskMSDN

你可以尝试做以下

首先为每个来电

var client = new WebClient(); 
client.Headers[HttpRequestHeader.ContentType] = "application/json"; 

Task<String> task1 = Task.Factory.StartNew(() => { 
    client.UploadString("http://localhost:45868/Product/GetAvailableProductsByContact", 
             jsonser1); 
}); 

冲洗的Task<String>和重复其他2

之后,您可以加入由执行3 Task<String>的调用task1.Valuetask2.Valuetask3.Value将返回一个String

+0

@mellamokb打败我吧!他/她的第二个答案很棒。 – foliveira 2012-03-23 18:21:47

+0

感谢您的回答! – Krishh 2012-03-23 18:26:03

+1

请注意,您希望在并行任务中共享“客户端”的相同实例。 'WebClient'实例没有被声明为线程安全的,这就是为什么我明确地在我的示例中努力为每个服务调用创建'WebClient'对象的单独实例。 – mellamokb 2012-03-23 18:26:30

1

可以使用begin/end pattern:

此示例显示如何并行调用两个ws。你可以将这个想法扩展到N个服务。

相关问题