WHO WILL BENEFIT FROM THIS TALK TOPICS WHAT YOU’LL LEAVE WITH ASP.NET developers, including Web Forms & MVC History of async programming in.NET How async works in ASP.NET Using async in ASP.NET apps How to use async programming in ASP.NET to increase scalability, improve page response time and implement long running requests The Task Parallel Library (TPL) and async/await support in in C# and.NET 4.5 makes programming async much easier than traditional APM ASP.NET 4.5, including the core framework, Web Forms and MVC, has great support for working with the TPL and async/await
A brief history of async programming in.NET
Asynchronous Programming Model (APM) Evented Asynchronous Programming (EAP) Task-based Asynchronous Programming (TAP)
//.NET 1 model file.BeginRead(buffer, 0, maxLength, asyncResult => { int numBytesRead = file.EndRead(asyncResult); // Now do something with "buffer“ }, null); Asynchronous Programming Model (APM)
//.NET 2 model webClient.DownloadStringCompleted += (sender, args) => { string html = args.Result; // Now do something with "html" }; webClient.DownloadStringAsync(new Uri("
Task htmlTask = webClient.DownloadStringTaskAsync(url); htmlTask.ContinueWith(task => { string html = task.Result; // Async, C# 4 }); string html = htmlTask.Result; // Sync (block until done) string html = await htmlTask; // Async, C# 5
public async Task MyMethod() { string myParam = "some value"; var data = await FetchSomeData(myParam); return View(data); } 2 1 public Task MyMethod() { string myParam = "some value"; return FetchSomeData(myParam).ContinueWith(task => { var data = task.Result; return View(data); }); } 2 1 Before compilation After compilation (conceptual)
How async requests work in ASP.NET
Traditional Web request handling “thread-per-request” a.k.a. “post office” Thread pool Requests Busy
Asynchronous Web request handling a.k.a. “restaurant” Requests Thread pool
Using async for benefit in ASP.NET apps. Easy as 1, 3, 2
TOOL-810T: Async made simple in Windows 8, with C# and Visual Basic RELATED SESSIONS DOCUMENTATION & ARTICLES