Asynchrony in (ASP).NET Aliaksandr Famin [email_address] @AlexSane
Developer and a Kettle
Developer and a Kettle <ul><li>Wait </li></ul><ul><li>Make someone to wait </li></ul><ul><li>Check periodically </li></ul>...
Comparison Approach Transferring 1K, rps Transferring 1M, rps Sync 2844,44 100% 32,57 26% Async with callbacks 2547,26 90%...
Evolution: WinAPI <ul><li>struct OVERLAPPED (IAsyncResult) </li></ul><ul><li>OVERLAPPED_COMPLETION_ROUTINE  (AsyncCallback...
Evolution: .NET 1.0 – 1.1 <ul><li>IAsyncResult </li></ul><ul><li>AsyncCallback </li></ul><ul><li>BeginXXX(@params, callbac...
Evolution: .NET 2.0 – …  <ul><li>event XXXCompleted </li></ul><ul><li>XXXCompletedEventArgs </li></ul><ul><li>EventHandler...
Evolution: .NET 4.0 <ul><li>Task Parallel Library </li></ul>
IAsyncResult vs Events <ul><li>IAsyncResult </li></ul><ul><li>Callbacks are not thread safe </li></ul><ul><li>No context i...
Event-based async pattern SyncronizationContext Thread #1 XAsync YAsync Thread #3 YCompleted Thread #2 ZAsync Thread #4 ZC...
TTT #1: Use closures <ul><li>var  asyncResult = component.BeginSomeOperation(parameter, </li></ul><ul><li>aresult => Trick...
TTT#2: Use userState carefully <ul><li>static   void  Main( string [] args) </li></ul><ul><li>{ </li></ul><ul><li>var  asy...
TTT#3: Unsubscribe from events <ul><li>static   void  Main( string [] args) </li></ul><ul><li>{ </li></ul><ul><li>componen...
TTT#5: There is no timeout support in both async patterns
TTT#6 AsyncResult callbacks don’t have HttpContext <ul><li>It could be, but there is no guarantee </li></ul>
TTT#7 Do not convert event-based pattern to IAsyncResult one <ul><li>Event-based pattern uses locks, that could be not des...
TTT #8: Do not use [ThreadStatic] <ul><li>Do not forget about ThreadPool </li></ul>
TTT#9: Do not throw exceptions in callbacks <ul><li>Even WSE 3.0 has had a bug we had spotted. </li></ul>
Q&A
Upcoming SlideShare
Loading in...5
×

Evolution of asynchrony in (ASP).NET

1,606

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,606
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Evolution of asynchrony in (ASP).NET

  1. 1. Asynchrony in (ASP).NET Aliaksandr Famin [email_address] @AlexSane
  2. 2. Developer and a Kettle
  3. 3. Developer and a Kettle <ul><li>Wait </li></ul><ul><li>Make someone to wait </li></ul><ul><li>Check periodically </li></ul><ul><li>Make someone to check </li></ul><ul><li>Put a whistle on a kettle </li></ul><ul><li>Add a web-interface to a kettle </li></ul><ul><li>Sync </li></ul><ul><li>Sync with threads </li></ul><ul><li>Async-sync </li></ul><ul><li>Async with threads </li></ul><ul><li>Async with callbacks </li></ul><ul><li>Async with events </li></ul>
  4. 4. Comparison Approach Transferring 1K, rps Transferring 1M, rps Sync 2844,44 100% 32,57 26% Async with callbacks 2547,26 90% 125,97 100% Async - sync 2327,28 82% 33,04 26% Sync with threads 861,23 30% 83,89 67% Async with threads 604,84 21% 80,36 64%
  5. 5. Evolution: WinAPI <ul><li>struct OVERLAPPED (IAsyncResult) </li></ul><ul><li>OVERLAPPED_COMPLETION_ROUTINE (AsyncCallback) </li></ul>
  6. 6. Evolution: .NET 1.0 – 1.1 <ul><li>IAsyncResult </li></ul><ul><li>AsyncCallback </li></ul><ul><li>BeginXXX(@params, callback, userState) </li></ul><ul><li>EndXXX(asyncResult) </li></ul>
  7. 7. Evolution: .NET 2.0 – … <ul><li>event XXXCompleted </li></ul><ul><li>XXXCompletedEventArgs </li></ul><ul><li>EventHandler<XXXCompletedEventArgs> </li></ul><ul><li>XXXAsync(@params, userState) </li></ul><ul><li>CancelAsync() </li></ul><ul><li>AsyncOperationManager </li></ul><ul><li>AsyncOperation </li></ul><ul><li>SyncronizationContext </li></ul>
  8. 8. Evolution: .NET 4.0 <ul><li>Task Parallel Library </li></ul>
  9. 9. IAsyncResult vs Events <ul><li>IAsyncResult </li></ul><ul><li>Callbacks are not thread safe </li></ul><ul><li>No context in callbacks (i.e. no HttpContext) </li></ul><ul><li>Complexity of chaining (custom IAsyncResult, etc.) </li></ul><ul><li>Poor support of components </li></ul><ul><li>Events </li></ul><ul><li>Event handlers are under lock </li></ul><ul><li>HttpContext available </li></ul><ul><li>Chaining is simple </li></ul><ul><li>Component-oriented </li></ul>ContextAwareResult, but no guarantee
  10. 10. Event-based async pattern SyncronizationContext Thread #1 XAsync YAsync Thread #3 YCompleted Thread #2 ZAsync Thread #4 ZCompleted AsyncOperationManager PreRenderComplete PreRender X Y XCompleted Z All operations are completed
  11. 11. TTT #1: Use closures <ul><li>var asyncResult = component.BeginSomeOperation(parameter, </li></ul><ul><li>aresult => TrickyCallback(aresult, component), </li></ul><ul><li>state); </li></ul><ul><li>void TrickyCallback( IAsyncResult state, Component component) </li></ul><ul><li>{ </li></ul><ul><li>bool result = component.EndOperation(state); </li></ul><ul><li>} </li></ul>
  12. 12. TTT#2: Use userState carefully <ul><li>static void Main( string [] args) </li></ul><ul><li>{ </li></ul><ul><li>var asyncResult = component.BeginSomeOperation( </li></ul><ul><li>parameter, </li></ul><ul><li>Callback, </li></ul><ul><li>state); </li></ul><ul><li>} </li></ul><ul><li>void Callback( IAsyncResult aresult) </li></ul><ul><li>{ </li></ul><ul><li>bool result = component.EndOperation(aresult); </li></ul><ul><li>object state = asyncResult.AsyncState; </li></ul><ul><li>} </li></ul>
  13. 13. TTT#3: Unsubscribe from events <ul><li>static void Main( string [] args) </li></ul><ul><li>{ </li></ul><ul><li>component.OperationCompleted += </li></ul><ul><li>component_OperationCompleted; </li></ul><ul><li>component.OperationAsync( &quot;data&quot; , null ); </li></ul><ul><li>} </li></ul><ul><li>static void component_OperationCompleted( object sender, EventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>component.OperationCompleted -= </li></ul><ul><li>component_OperationCompleted; </li></ul><ul><li>} </li></ul>
  14. 14. TTT#5: There is no timeout support in both async patterns
  15. 15. TTT#6 AsyncResult callbacks don’t have HttpContext <ul><li>It could be, but there is no guarantee </li></ul>
  16. 16. TTT#7 Do not convert event-based pattern to IAsyncResult one <ul><li>Event-based pattern uses locks, that could be not desirable </li></ul>
  17. 17. TTT #8: Do not use [ThreadStatic] <ul><li>Do not forget about ThreadPool </li></ul>
  18. 18. TTT#9: Do not throw exceptions in callbacks <ul><li>Even WSE 3.0 has had a bug we had spotted. </li></ul>
  19. 19. Q&A
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×