3. Agenda
• Introduction on synchronicity
• Threading and async programming in .NET
• Details for
• ASP.NET WebForms
• ASP.NET MVC
• ASP.NET WebAPI
• ASP.NET SignalR
• Gotchas
• Questions and Answers
5. (A)Synchronous in a web world
Message
Exchange
Patterns
Parallelization
vs. multi-
threading
High Latency
vs high
throughput
Asynchronicity
is easy now
Blocking
operations
Asynchronous
is faster
7. Threading in Windows and .NET
• Two concurrency types in Windows Operating
System
1. Worker threads
Physical units of work
2. IO Completion Ports
Special construct for async
I/O bound operations
• Threads incur overhead
• But threads waiting for IO Completion Port are efficient
8. .NET Threading primitives
• System.Threading namespace
• Threads: Thread and ThreadPool
• Locks: Mutex, WaitHandle, Semaphore, Monitor, Interlocked
• ThreadPool cannot scale hard
(2 extra threads/second)
• Each .NET logical thread adds overhead
(1MB of managed memory)
.NET 4.5 Worker Threads Completion Port
Threads
Minimum 4 4
Maximum 5000 (4095) 1000
9. Threading in ASP.NET
Application Pool (w3wp.exe)
CLR Threadpool
Request Queue
AppDomain
Website
http.sys
IIS
Unmanaged execution
Kernel level
… …
Global Queue
connectionManagement
maxconnection
httpRuntime/
minFreeThreads
minLocalRequestFreeThreads
processModel/
maxWorkerThreads
minWorkerThreads
maxIoThreads
Outgoing connections
applicationPool/
maxConcurrentRequestsPerCpu
Worker
Threads
Completion
Port Threads
10. Making a choice for (a)sync
Synchronous
• Operations are simple or
short-running
• Simplicity over efficiency
• CPU-bound operations
Asynchronous
• Ability to cancel long-
running tasks
• Parallelism over simplicity
• Blocking operations are
bottleneck for
performance
• Network or I/O bound
operations
11. Worker thread #1 Worker thread #2Worker thread #3 IO Completion Port
Thread #3
Threads types and context switches
ASP.NET
Runtime
Store
Customers
Async
Windows IO
Completion
Port
db.Save
ChangesAsync
Must support async pattern
in some way
As an example,
Entity Framework 6 has
support for async operations
Unnecessary additional
threads only occur overhead.
Underlying SqlClient uses IO
Completion Port for async I/O
operation
13. History of .NET async programming
Asynchronous Programming ModelAPM
• Pairs of Begin/End methods
• Example: FileStream.BeginWrite and FileStream.EndWrite
• Convert using TaskFactory and TaskFactory<TResult>
Event-based Asynchronous PatternEAP
• Pairs of OperationAsync method and OperationCompleted event
• Example: WebClient.DownloadStringAsync and WebClient.DownloadStringCompleted
• TaskCompletionSource<T> to the rescue
Task-based Asynchronous PatternTAP
• Task and Task<T>
• Preferred model
14. Async in .NET BCL classes
• .NET Framework classes show each async style
• Sometimes even mixed
• Example: System.Net.WebClient
• TPL (Task-based) APIs are preferred
• Find and use new classes that support TAP natively
15. Async constructs in ASP.NET
• ASP.NET runtime
• Async Modules
• Async Handlers
• ASP.NET WebForms
• AddOnPreRenderComplete
• PageAsyncTask
• ASP.NET MVC and WebAPI
• Async actions
20. Async pages in WebForms
Recipe for async pages
• Add async="true" to @Page directive or <pages> element in
web.config
• Pass delegates for start and completion of asynchronous operation
in AddOnPreRenderCompleteAsync method
• Register event handler for PreRenderComplete
private void Page_Load(object sender, EventArgs e)
{
this.AddOnPreRenderCompleteAsync(
new BeginEventHandler(BeginAsynchronousOperation),
new EndEventHandler(EndAsynchronousOperation));
this.PreRenderComplete += new
EventHandler(LongRunningAsync_PreRenderComplete);
}
21. PageAsyncTasks
• Single unit of work
• Encapsulated by PageAsyncTask class
• Support for APM and TPL (new in ASP.NET 4.5)
• Preferred way over async void event handlers
• Can run multiple tasks in parallel
// TAP async delegate as Page task
RegisterAsyncTask(new PageAsyncTask(async (token) =>
{
await Task.Delay(3000, token);
}));
23. Async support in MVC
• AsyncController (MVC3+)
• Split actions in two parts
1. Starting async: void IndexAsync()
2. Completing async: ActionResult IndexCompleted(…)
• AsyncManager
• OutstandingOperations Increment and Decrement
• Task and Task<ActionResult> (MVC 4+)
• Async and await (C# 5+)
24. Async actions in ASP.NET MVC
From synchronous
public ActionResult Index()
{
// Call synchronous operations
return View("Index", GetResults());
}
To asynchronous
public async Task<ActionResult> IndexAsync()
{
// Call operations asynchronously
return View("Index", await GetResultsAsync());
}
25. Timeouts
• Timeouts apply to synchronous handlers
• Default timeout is 110 seconds
(90 for ASP.NET 1.0 and 1.1)
• MVC and WebAPI are always asynchronous
• Even if you only use synchronous handlers
• (Server script) timeouts do not apply
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5" executionTimeout="5000" />
</system.web>
26. Timeouts in MVC and WebAPI
• Use AsyncTimeoutAttribute on async actions
• When timeout occurs TimeoutException is thrown
from action
• Default timeout is AsyncManager’s 45000 milliseconds
• Might want to catch errors
[AsyncTimeout(2000)]
[HandleError(ExceptionType=typeof(TimeoutException))]
public async Task<ActionResult> SomeMethodAsync(CancellationToken token)
{
// Pass down CancellationToken to other async method calls
…
}
27. ASP.NET SignalR async notes
• In-memory message bus is very fast
• Team decided not to make it async
• Sending to clients is
• always asynchronous
• on different call-stack
28. Beware of the async gotchas
• Cannot catch exceptions in async void methods
• Mixing sync/async can deadlock threads in ASP.NET
• Suboptimal performance for regular awaits
Best practices for async:
• Avoid async void
• Async all the way
• Configure your wait
29. Tips
• Don’t do 3 gotcha’s
• Avoid blocking threads and thread starvation
• Remember I/O bound (await)
vs. CPU bound (ThreadPool, Task.Run, Parallel.For)
• Thread management:
• Avoid creating too many
• Create where needed and reuse if possible
• Try to switch to IO Completion Port threads
• Look for BCL support
30. Summary
• Make sure you are comfortable with
• Multi-threading, parallelism, concurrency, async and await
• ASP.NET fully supports TPL and async/await
• WebForms
• MVC
• WebAPI
• Great performance and scalability comes from
good thread management
• Be aware of specific ASP.NET behavior