Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All.......

I am new in .Net development..........

I want to call two web service methods simultaneously(parallel) using c# code from the application, web service is built as a separate application. In my application, one web service method should get called & it is not needed to wait for the response, then another service method should get called & it is not needed to wait for the response, but after calling both of these service methods, I need to wait for the respond for first method & as per the result I need to proceed ahead, what is the best way to perform this?

Thanks in advance.

Regards
Posted
Comments
RAJKUMAR M G 7-Aug-15 6:25am    
Try to tell clearly...

The simplest solution would be to use the HttpClient class then make async calls and wait for those tasks to complete. See sample below.

C#
static void Run()
{
    //Follow steps at this link for addding a reference to the necessary .NET library:
    //http://stackoverflow.com/questions/9611316/system-net-http-missing-from-    
    //namespace-using-net-4-5

    //Create an HTTP Client
    var client = new HttpClient();

    //Call first service
    var task1 = client.GetAsync("http://www.cnn.com");

    //Call second service
    var task2 = client.GetAsync("http://www.google.com");

    //Create list of all returned async tasks
    var allTasks = new List<Task<HttpResponseMessage>> { task1, task2 };

    //Wait for all calls to return before proceeding
    Task.WaitAll(allTasks.ToArray());

}
 
Share this answer
 
Comments
Richard Deeming 7-Aug-15 14:34pm    
Why would you create a new List<...> if you're just going to immediately convert it to an array? Just create the array directly:
var allTasks = new[] { task1, task2 };

Alternatively, take advantage of the fact that the WaitAll parameter is declared as params:
Task.WaitAll(task1, task2);

Your method should also return a Task, so that the calling method can wait for it to finish:
static Task Run()
{
...
return Task.WhenAll(task1, task2);
}
kellyapt1 7-Aug-15 18:23pm    
Good recommendations (especially around the params). With the "try to tell it clearly...", I thought that keeping it simple in terms of not returning another task would make things simpler to understand.

If I were to return a Task from this method I would make it generic and return an array of HttpResponseMessages:

static Task<httpresponsemessage[]> Run()
{
...
return Task.WhenAll(task1, task2);
}
Good recommendations (especially around the params). With the "try to tell it clearly...", I thought that keeping it simple in terms of not returning another task would make things simpler to understand.

If I were to return a Task from this method I would make it generic and return an array of HttpResponseMessages:


XML
static Task<HttpResponseMessage[]> Run()
{
    ...
    return Task.WhenAll(task1, task2);
}
 
Share this answer
 
Comments
Matt T Heffron 7-Aug-15 18:17pm    
This really should have been a reply to Richard's comments on your other Solution.
He'll not see this unless he happens to return to this question.
As a reply, he'd have gotten an email notification.
Posting this another Solution looks like abuse of the reputation points system.
kellyapt1 7-Aug-15 18:25pm    
Fair enough. Did not know that I can reply directly like that - first time really doing anything in this forum. Lesson learned. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900