Skip to content

Instantly share code, notes, and snippets.

@DamianEdwards
Created November 7, 2012 09:31
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save DamianEdwards/4030394 to your computer and use it in GitHub Desktop.
Save DamianEdwards/4030394 to your computer and use it in GitHub Desktop.
Async site scraping in ASP.NET 4.5
public class SiteScrape : HttpTaskAsyncHandler
{
public override async Task ProcessRequestAsync(HttpContext context)
{
using (var http = new HttpClient())
{
var downloadTasks = new List<Task<string>> {
http.GetStringAsync("http://bing.com"),
http.GetStringAsync("http://google.com"),
http.GetStringAsync("http://oredev.org"),
http.GetStringAsync("http://microsoft.com"),
http.GetStringAsync("http://asp.net"),
http.GetStringAsync("http://signalr.net")
};
while (downloadTasks.Count > 0)
{
var completed = await Task.WhenAny(downloadTasks);
downloadTasks.Remove(completed);
context.Response.Write(completed.Result);
await context.Response.FlushAsync();
}
}
}
}
@DamianEdwards
Copy link
Author

The goodness of async/await in ASP.NET 4.5. This asynchronously downloads the HTML from all the sites in parallel and as each one returns, asynchronously flushes it to the response, i.e. it can be flushing back HTML to the browser while still downloading HTML from the other sites, in parallel.

I'd have no hope of writing this in APM (Begin/End).

@r4vi
Copy link

r4vi commented Nov 27, 2012

hotness

@leniel
Copy link

leniel commented Nov 27, 2012

Just AMAZING... :D

@micahasmith
Copy link

I never knew about HttpTaskAsyncHandler until now. The fact that it allows you to do IsReusable="true" is awesome. Can't wait to benchmark this thing.

Thanks for posting!

@justinmchase
Copy link

Throw some linq in there while you're at it:

while(downloadTasks.Any()) { ... }

:)

@Meligy
Copy link

Meligy commented Feb 20, 2014

Would this work with Response.BufferOutput = false; if you are getting a Stream not a string?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment