KEMBAR78
Asynchronous programming in ASP.NET
Asynchronous
programming in ASP.NET
Name:
Role:
https://nl.linkedin.com/in/alexthissen
@alexthissen
http://blog.alexthissen.nl
Alex Thissen
Lead Consultant, Xpirit
Introducing myself
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
Talking about
synchronicity
Primer on async and multi-threading in Windows and .NET
(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
A tale of fast food restaurants
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
.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
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
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
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
Asynchronous
programming
.NET Framework support for async
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
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
Async constructs in ASP.NET
• ASP.NET runtime
• Async Modules
• Async Handlers
• ASP.NET WebForms
• AddOnPreRenderComplete
• PageAsyncTask
• ASP.NET MVC and WebAPI
• Async actions
ASP.NET WebForms
ASP.NET specifics part 1
Asynchronous programming in
WebForms
Your options:
1. Asynchronous
pages
2. AsyncTasks
It all comes down to hooking into async page lifecycle
Normal synchronous page lifecycle
for ASP.NET WebForms
…
…
LoadComplete
PreRender
PreRenderComplete
SaveViewState
Client
Request
page
Send response
Render
…
Thread 1
Switch to asynchronous handling
…
…
LoadComplete
PreRender
PreRenderComplete
SaveViewState
Client
Send response
Render
…
Thread 1
Thread 2
IAsyncResult
Request
page
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);
}
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);
}));
ASP.NET MVC and
WebAPI
ASP.NET specifics part 2
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+)
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());
}
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>
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
…
}
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
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
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
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
Questions? Answers!

Asynchronous programming in ASP.NET

  • 1.
  • 2.
  • 3.
    Agenda • Introduction onsynchronicity • Threading and async programming in .NET • Details for • ASP.NET WebForms • ASP.NET MVC • ASP.NET WebAPI • ASP.NET SignalR • Gotchas • Questions and Answers
  • 4.
    Talking about synchronicity Primer onasync and multi-threading in Windows and .NET
  • 5.
    (A)Synchronous in aweb world Message Exchange Patterns Parallelization vs. multi- threading High Latency vs high throughput Asynchronicity is easy now Blocking operations Asynchronous is faster
  • 6.
    A tale offast food restaurants
  • 7.
    Threading in Windowsand .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 ApplicationPool (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 choicefor (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 #1Worker 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
  • 12.
  • 13.
    History of .NETasync 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 .NETBCL 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 inASP.NET • ASP.NET runtime • Async Modules • Async Handlers • ASP.NET WebForms • AddOnPreRenderComplete • PageAsyncTask • ASP.NET MVC and WebAPI • Async actions
  • 16.
  • 17.
    Asynchronous programming in WebForms Youroptions: 1. Asynchronous pages 2. AsyncTasks It all comes down to hooking into async page lifecycle
  • 18.
    Normal synchronous pagelifecycle for ASP.NET WebForms … … LoadComplete PreRender PreRenderComplete SaveViewState Client Request page Send response Render … Thread 1
  • 19.
    Switch to asynchronoushandling … … LoadComplete PreRender PreRenderComplete SaveViewState Client Send response Render … Thread 1 Thread 2 IAsyncResult Request page
  • 20.
    Async pages inWebForms 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 unitof 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); }));
  • 22.
  • 23.
    Async support inMVC • 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 inASP.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 applyto 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 MVCand 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 asyncnotes • 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 theasync 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 do3 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 sureyou 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
  • 31.

Editor's Notes

  • #10 http://support.microsoft.com/kb/821268
  • #11 http://www.asp.net/web-forms/overview/performance-and-caching/using-asynchronous-methods-in-aspnet-45
  • #22 http://blogs.msdn.com/b/pfxteam/archive/2012/05/31/what-s-new-for-parallelism-in-visual-studio-2012-rc.aspx
  • #28 http://stackoverflow.com/questions/19193451/should-signalr-server-side-methods-be-async-when-calling-clients
  • #29 http://msdn.microsoft.com/en-us/magazine/jj991977.aspx