1

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?

asked Mar 23, 2012 at 17:57
1
  • Put this into another class and multithread it? Commented Mar 23, 2012 at 18:06

3 Answers 3

5

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];
answered Mar 23, 2012 at 18:13
2
  • Thank you very much for your answer. This indeed would speed up the process. Commented Mar 23, 2012 at 18:25
  • 1
    I 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. Commented Mar 23, 2012 at 18:29
1

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.

answered Mar 23, 2012 at 18:35
0

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

answered Mar 23, 2012 at 18:20
2
  • @mellamokb beat me to it! His/her 2nd answer is great. Commented Mar 23, 2012 at 18:21
  • 1
    Note 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 of WebClient objects for each service call. Commented Mar 23, 2012 at 18:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.