I need to call 3 WCF services as below,
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);
This takes a lot of time to get results and display on the page. Can anyone help me how I could execute them in parallel?
-
Put this into another class and multithread it?user1247139– user12471392012年03月23日 18:06:25 +00:00Commented Mar 23, 2012 at 18:06
3 Answers 3
You can use the 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;
Or alternatively, since your requests are all very similar, differing only by the url and upload string, you can use LINQ AsParallel
array processing:
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];
-
Thank you very much for your answer. This indeed would speed up the process.Krishh– Krishh2012年03月23日 18:25:39 +00:00Commented Mar 23, 2012 at 18:25
-
1I did not test any of the code above. Let me know if it generates compile-time or runtime errors. You may also want to familiarize yourself with
AggregateException
for dealing with errors when one or more of the requests cannot be completed.mellamokb– mellamokb2012年03月23日 18:29:42 +00:00Commented Mar 23, 2012 at 18:29
You can use the begin/end pattern:
This example shows you how to call two ws in parallell. You can extend the idea to N services.
Try creating a Task
that executes each of your calls (MSDN)
You could try doing the following
First create a Task<String>
for each of your calls
var client = new WebClient();
client.Headers[HttpRequestHeader.ContentType] = "application/json";
Task<String> task1 = Task.Factory.StartNew(() => {
client.UploadString("http://localhost:45868/Product/GetAvailableProductsByContact",
jsonser1);
});
Rinse and repeat for the other 2
Afterwards you can join on the execution of the 3 Task<String>
by calling task1.Value
, task2.Value
and task3.Value
which will return a String
-
@mellamokb beat me to it! His/her 2nd answer is great.foliveira– foliveira2012年03月23日 18:21:47 +00:00Commented Mar 23, 2012 at 18:21
-
1Note that you want to be careful about sharing the same instance of the
client
across the parallel tasks.WebClient
instances are not declared thread safe, which is why I explicitly go to the effort in my examples to create separate instances ofWebClient
objects for each service call.mellamokb– mellamokb2012年03月23日 18:26:30 +00:00Commented Mar 23, 2012 at 18:26
Explore related questions
See similar questions with these tags.