1

I've got an action on my website that requires me to check multiple web servers and aggregate the results. Currently this is done synchronously against each web server in a foreach which can be slow depending on how responsive the web-server is.

What Id like to do is each request asynchronously and aggregate all the results when they are all complete.

My question is - with ASP.NET Webforms, what's the best way to do this? Is there anything I should avoid?

Each request could take 2 seconds so I'd like to kick them all off at the same time.

6
  • Which version of ASP.NET are you using? Commented Oct 23, 2012 at 19:15
  • See forums.asp.net/t/1331312.aspx/1 Commented Oct 23, 2012 at 19:24
  • @JohnSaunders It's so, so much easier if you have access to .NET 4.5 though, hence Kiran's comment. Commented Oct 23, 2012 at 19:28
  • @pst: asp.net/web-forms/tutorials/aspnet-45/… Commented Oct 23, 2012 at 19:30
  • @JohnSaunders That doesn't seem to have anything to do with "making it faster" :( This problem seems more suitable for TPL or similar parallelism, not necessarily across requests (e.g. reduce other response times under load). Commented Oct 23, 2012 at 19:31

3 Answers 3

2

This sounds like a good care for "standard" asynchronous IO or "standard" threaded IO.

Where T represents {time1, time2, ..}, this would make the response O(max(T)) instead of O(sum(T)) - assuming no sequential dependencies or other bottlenecks.

This can be done either using either

Asynchronous IO - where the IO does not block the thread

or Synchronous IO, but threaded - where the IO does block an individual thread

  • TPL in .NET4
  • or "old manual" threading/pooling (linked post has same "requirement")

The .NET version used and the API available to get the external data plays a factor in which approach is [more] appropriate.

Sign up to request clarification or add additional context in comments.

1 Comment

I think im going to go with the async/await pattern - it seems the best choice!
0

I would use jQuery to make the asynchronous AJAX calls to the server. However, I would use an ashx handler instead of a WebForm to process the AJAX calls on the server and send back the data to the client; ashx handlers are more light weight than WebForms. Here is a quick tutorial on writing ASHX handlershttp://www.dotnetperls.com/ashx Is this helping you?

Comments

0

If you are using .NET framework 4, you may use Task parallel library to achieve this. Assuming GetDataFromServer is the method you have to call for different servers, you may perform a piece of logic similar to the following logic to get data from all servers:

var d1 = Task.Factory.StartNew(() => GetDataFromServer("server1"));
var d2 = Task.Factory.StartNew(() => GetDataFromServer("server2"));
var d3 = Task.Factory.StartNew(() => GetDataFromServer("server3"));
var d4 = Task.Factory.StartNew(() => GetDataFromServer("server4"));

TaskCoordinator.WhenAll(new Task[] {d1, d2, d3, d4});

The above step calls all the four methods asynchronously. It will wait for all of them to complete. Result of each operation is stored in Result property of the corresponding Task object. So, you can get the results using d1.Result, d2.Result and so on to perform your logic on the results obtained from the method calls.

Comments

Your Answer

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.