Contenu connexe Similaire à Async ASP.NET Applications (20) Async ASP.NET Applications2. 2
© DEVintersection. All rights reserved.
http://www.DEVintersection.com
Agenda
Quick History
When is Async Useful
Parallelism
Tips & Resources
3. 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. 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. 5
© DEVintersection. All rights reserved.
http://www.DEVintersection.com
Async in .NET
.NET Framework 4.0
TPL: Task Parallel Library
Task<int> t = ProcessAsync(…);
// process other work
// check t.Status, t.IsFaulted, etc.
int result = t.Result;
6. 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. 7
© DEVintersection. All rights reserved.
http://www.DEVintersection.com
When to Async
When is it most useful?
I/O
Parallelism
Long Running Tasks
8. 8
© DEVintersection. All rights reserved.
http://www.DEVintersection.com
Application Architecture
50 States
Web API
Web Forms
Application
Warnings db
10. 10
© DEVintersection. All rights reserved.
http://www.DEVintersection.com
WebForms
Notes
DO NOT async your Page_Load
RegisterAsyncTask
Set Async=“true” on your .aspx page to enable async pipeline
12. 12
© DEVintersection. All rights reserved.
http://www.DEVintersection.com
MVC / Web API
Notes
Simple & Transparent
Mark method as async, return Task<T>
Use Entity Framework >= 6.1 for async methods
13. 13
© DEVintersection. All rights reserved.
http://www.DEVintersection.com
Parallelism
Task.WhenAll
Task.Run, ThreadPool.QueueUserWorkItem
15. 15
© DEVintersection. All rights reserved.
http://www.DEVintersection.com
Final Comments
Avoid using Background Threads
Do not use Task.Wait()
Avoid Using Task.ContinueWith
Parallel != Async
16. 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.5
http://www.asp.net/web-forms/tutorials/aspnet-45/using-asynchronous-
methods-in-aspnet-45
Contact Information