DEVintersectionSession AS12
Building Asynchronous ASP.NET ApplicationsShayne Boyer
[email protected]@spboyer / tattoocoder.com
2© DEVintersection. All rights reserved.
http://www.DEVintersection.com
Agenda
Quick History When is Async Useful Parallelism Tips & Resources
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);
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(…);
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;
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;}
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
8© DEVintersection. All rights reserved.
http://www.DEVintersection.com
Application Architecture
50 States
Web APIWeb FormsApplication
Warnings db
Demo
WebFormsUsing Async in WebForm 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
Demo
MVC / Web APIUsing Async in MVC
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
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
Demo
Parallelism
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
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
Questions?
Thank you!
Don’t forget to enter your evaluation of this session using EventBoard!
Top Related