21
www.buildwindows.com Building IIS and ASP.NET apps with the power of async Damian Edwards, Phil Haack Program Managers, ASP.NET Microsoft Corporation SAC-804T

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

Embed Size (px)

Citation preview

Page 1: 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

www.buildwindows.com

Building IIS and ASP.NET apps with the power of async

Damian Edwards, Phil HaackProgram Managers, ASP.NETMicrosoft Corporation

SAC-804T

Page 2: 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

www.buildwindows.com

Agenda slide

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

Page 3: 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

A brief history of async programming in .NET

Page 4: 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

www.buildwindows.com

Three async programming models

Asynchronous Programming

Model(APM)

Evented Asynchronous Programming

(EAP)

Task-based Asynchronous Programming

(TAP)

Page 5: 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

// .NET 1 model

file.BeginRead(buffer, 0, maxLength, asyncResult => {

int numBytesRead = file.EndRead(asyncResult); // Now do something with "buffer“

}, null);

Asynchronous Programming Model (APM)Asynchronous Programming Model (APM)

Page 6: 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

// .NET 2 model

webClient.DownloadStringCompleted += (sender, args) => {

string html = args.Result; // Now do something with "html"

};webClient.DownloadStringAsync(new Uri("http://example.com"));

Event-based Asynchronous Programming (EAP)

Page 7: 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

Task<string> 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

Task-based Asynchronous Programming (TAP)

Page 8: 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

public async Task<ViewResult> MyMethod(){ string myParam = "some value"; var data = await FetchSomeData(myParam); return View(data);} 2

1

public Task<ViewResult> 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 C# 5 “async” works

Page 9: 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

How async requests work in ASP.NET

Page 10: 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

www.buildwindows.com

Traditional Web request handling“thread-per-request” a.k.a. “post office”

Thread pool

Requests

BusyBusy Busy Busy

Page 11: 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

www.buildwindows.com

Asynchronous Web request handlinga.k.a. “restaurant”

Requests

Thread pool

Page 12: 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

Using async for benefit in ASP.NET apps. Easy as 1, 3, 2

Page 13: 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

So can I just use async everywhere in my ASP.NET app? No!

Page 14: 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

There are three (3) distinct scenarios where async in ASP.NET apps might be useful…

Page 15: 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

www.buildwindows.com

Async IO in ASP.NET

demo

Page 16: 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

www.buildwindows.com

Parallelizing work for faster request throughput

demo

Page 17: 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

www.buildwindows.com

Handling long running, event driven requests

demo

Page 18: 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

www.buildwindows.com

For more information

• TOOL-810T: Async made simple in Windows 8, with C# and Visual Basic

RELATED SESSIONS DOCUMENTATION & ARTICLES

• http://www.asp.net/vnext

Page 19: 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

www.buildwindows.com

• Feedback and questions http://forums.dev.windows.com

• Session feedbackhttp://bldw.in/SessionFeedback

thank you

Page 20: 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

© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to

be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 21: 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