Async ASP.NET Applications

17
DEVintersection Session AS12 Building Asynchronous ASP.NET Applications Shayne Boyer [email protected] @spboyer / tattoocoder.com

description

Learn how to take advantage of the new async/await operations to keep your applications responsive and avoid the performance bottlenecks. In comparison to how async code has been written with callbacks, the new way allows for cleaner more readable debug-able code. In this presentation, topics covered: Web Forms, MVC, EF6 some tooling.

Transcript of Async ASP.NET Applications

Page 1: Async ASP.NET Applications

DEVintersectionSession AS12

Building Asynchronous ASP.NET ApplicationsShayne Boyer

[email protected]@spboyer / tattoocoder.com

Page 2: Async ASP.NET Applications

2© DEVintersection. All rights reserved.

http://www.DEVintersection.com

Agenda

Quick History When is Async Useful Parallelism Tips & Resources

Page 3: Async ASP.NET Applications

3© DEVintersection. All rights reserved.

http://www.DEVintersection.com

Async in .NET.NET Framework 1.0 / 1.1

1.0 – no async available 1.1 – APM: Async Programming Model

IAsyncResult result = StartProcess(..);

// continue doing other work// checking result.IsCompleted

int final = EndProcess(result);

Page 4: Async ASP.NET Applications

4© DEVintersection. All rights reserved.

http://www.DEVintersection.com

Async in .NET.NET Framework 2.0

EAP: Event-base Async Pattern

ProcessCompleted += (sender, e) =>{ // process the result here};AsyncProcessStart(…);

Page 5: Async ASP.NET Applications

5© DEVintersection. All rights reserved.

http://www.DEVintersection.com

Async in .NET.NET Framework 4.0

TPL: Task Parallel Library Returns Task<TResult> Makes composition easy with .ContinueWith, .WhenAll. etc

Task<int> t = ProcessAsync(…);

// process other work// check t.Status, t.IsFaulted, etc.

int result = t.Result;

Page 6: Async ASP.NET Applications

6© DEVintersection. All rights reserved.

http://www.DEVintersection.com

Async in .NET.NET Framework 4.5

async/await keywords

public async Task<int> Process() {

int result = await CompleteWork();

return result;}

Page 7: Async ASP.NET Applications

7© DEVintersection. All rights reserved.

http://www.DEVintersection.com

When to AsyncWhen is it most useful?

I/O Database File Network

Parallelism Long Running Tasks

Page 8: Async ASP.NET Applications

8© DEVintersection. All rights reserved.

http://www.DEVintersection.com

Application Architecture

50 States

Web APIWeb FormsApplication

Warnings db

Page 9: Async ASP.NET Applications

Demo

WebFormsUsing Async in WebForm Applications

Page 10: Async ASP.NET Applications

10© DEVintersection. All rights reserved.

http://www.DEVintersection.com

WebFormsNotes

DO NOT async your Page_Load RegisterAsyncTask Set Async=“true” on your .aspx page to enable async pipeline

Page 11: Async ASP.NET Applications

Demo

MVC / Web APIUsing Async in MVC

Page 12: Async ASP.NET Applications

12© DEVintersection. All rights reserved.

http://www.DEVintersection.com

MVC / Web APINotes

Simple & Transparent Mark method as async, return Task<T> Use Entity Framework >= 6.1 for async methods

SaveChangesAsync FirstAsync() etc

Page 13: Async ASP.NET Applications

13© DEVintersection. All rights reserved.

http://www.DEVintersection.com

Parallelism

Task.WhenAll Runs two or more async operations without using any

additional threads Task.Run, ThreadPool.QueueUserWorkItem

Multiple synchronous operations Each will use a new thread from ASP.NET thread pool

Page 14: Async ASP.NET Applications

Demo

Parallelism

Page 15: Async ASP.NET Applications

15© DEVintersection. All rights reserved.

http://www.DEVintersection.com

Final Comments

Avoid using Background Threads All threads come from the same pool, using background

threads takes threads from processing new requests. Do not use Task.Wait()

Can easily deadlock ASP.NET process Use Task.WhenAll instead

Avoid Using Task.ContinueWith Puts you on a new thread Cannot get back to initial context, UI thread etc.

Parallel != Async Can use them together or separate. With great power comes great responsibility

Page 16: Async ASP.NET Applications

16© DEVintersection. All rights reserved.

http://www.DEVintersection.com

Resource & Links

Hanselminutes Podcast – Everything .NET programmers know about Asynchronous Programming is wrong.http://hanselminutes.com/327/everything-net-programmers-know-about-asynchronous-programming-is-wrong

Using Asynchronous Methods in ASP.NET 4.5http://www.asp.net/web-forms/tutorials/aspnet-45/using-asynchronous-methods-in-aspnet-45

Contact Information Twitter: @spboyer Blog: http://www.tattoocoder.com

Page 17: Async ASP.NET Applications

Questions?

Thank you!

Don’t forget to enter your evaluation of this session using EventBoard!