0
Asynchronous Programmingin ASP.NETChris Dufour, ASP .NET MVPSoftware Architect, Compuware   Follow me @chrduf   http://www...
Agenda•   ASP .NET page lifecycle•   Load test synchronous application•   History of Async support in ASP.NET•   TAP (Task...
ASP.NET Page Lifecycle
demoLoad Test Synchronous Page
Sync vs. Async• Synchronous call   • Caller WAITS for method to complete   • “Blocking”   • Easy to program/understand• As...
Asynchronous History.NET 1.1 - APM (Asynchronous Programming Model)• Call BeginXxx to start doing work    • Returns IAsync...
Asynchronous History.NET 2.0 - EAP (Event-Based Asynchronous Pattern)• Call XxxAsync to start work     • Often no way to g...
ASP.NET Asynchronous Page Lifecycle
Asynchronous Data Bindingpublic partial class AsyncDataBind : System.Web.UI.Page{                                         ...
Asynchronous History.NET 4.0 – TPL (Task Parallel Library)• Call XxxAsync to start work     • Returns Task (or Task<T>) to...
Asynchronous History.NET 4.5 – TAP (Task-Based Asynchronous Pattern)• Works on top of TPL• Introduces 2 new contextual key...
TAPprotected void Page_Load(...){    int r = DoWork();}private int DoWork(){    DoSomeWork;    return 1;}
TAPprotected void Page_Load(...)   Protected async void Page_Load(...){                               {    int r = DoWork(...
demoAsynchronize the Application
Thank You
Upcoming SlideShare
Loading in...5
×

Asynchronous Programming in ASP.NET

3,584

Published on

Your website is done. Your webpages access data from a database or a web service and have a 1 to 2 second response time. After deploying the application your user interface is unresponsive and your server doesn’t scale.

In this presentation we will find out what’s happening to our website under scale and how we can use the new async/await support in .NET 4.5 to make our application more responsive under load.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,584
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
38
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Asynchronous Programming in ASP.NET"

  1. 1. Asynchronous Programmingin ASP.NETChris Dufour, ASP .NET MVPSoftware Architect, Compuware Follow me @chrduf http://www.linkedin.com/in/cdufour
  2. 2. Agenda• ASP .NET page lifecycle• Load test synchronous application• History of Async support in ASP.NET• TAP (Task-based Asynchronous Pattern)• Asynchronize the application• Load test asynchronous application
  3. 3. ASP.NET Page Lifecycle
  4. 4. demoLoad Test Synchronous Page
  5. 5. Sync vs. Async• Synchronous call • Caller WAITS for method to complete • “Blocking” • Easy to program/understand• Asynchronous call • Method returns immediately to caller and executes callback (continuation) when complete • “Non-blocking” • Run several methods concurrently • Scalability • Harder to program
  6. 6. Asynchronous History.NET 1.1 - APM (Asynchronous Programming Model)• Call BeginXxx to start doing work • Returns IAsyncResult which reflects status • Doesn’t always run async • Problems with thread affinity• Call EndXxx to get the actual result value• No built-in support for async pages in ASP .NET
  7. 7. Asynchronous History.NET 2.0 - EAP (Event-Based Asynchronous Pattern)• Call XxxAsync to start work • Often no way to get status while in-flight • Problems with thread affinity• Subscribe to XxxCompleted event to get result• Introduces support for Async pages in ASP.NET
  8. 8. ASP.NET Asynchronous Page Lifecycle
  9. 9. Asynchronous Data Bindingpublic partial class AsyncDataBind : System.Web.UI.Page{ void EndAsyncOperation(IAsyncResult ar) private SqlConnection _connection; { private SqlCommand _command; _reader = _command.EndExecuteReader(ar); private SqlDataReader _reader; } protected void Page_Load(object sender, EventArgs e) { protected void Page_PreRenderComplete(object sender, EventArgs e) if (!IsPostBack) { { Output.DataSource = _reader; // Hook PreRenderComplete event for data binding Output.DataBind(); this.PreRenderComplete += new EventHandler(Page_PreRenderComplete); } // Register async methods AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginAsyncOperation), public override void Dispose() new EndEventHandler(EndAsyncOperation) { ); if (_connection != null) _connection.Close(); } base.Dispose(); } } IAsyncResult BeginAsyncOperation (object sender, EventArgs e, } AsyncCallback cb, object state) { string connect = WebConfigurationManager.ConnectionStrings ["PubsConnectionString"].ConnectionString; _connection = new SqlConnection(connect); _connection.Open(); _command = new SqlCommand( "SELECT title_id, title, price FROM titles", _connection); return _command.BeginExecuteReader (cb, state); }
  10. 10. Asynchronous History.NET 4.0 – TPL (Task Parallel Library)• Call XxxAsync to start work • Returns Task (or Task<T>) to reflect in-flight status • Problems with thread affinity• No second method or eventTask<int> t = SomethingAsync(…);//do work, checking t.Statusint r = t.Result
  11. 11. Asynchronous History.NET 4.5 – TAP (Task-Based Asynchronous Pattern)• Works on top of TPL• Introduces 2 new contextual key words • Async marks a method as asynchronous • Await yields control while waiting on a task to complete• Lets us write sequential code which is not necessarily synchronous• Takes care of sticky threading & performance issues related to Task<T>
  12. 12. TAPprotected void Page_Load(...){ int r = DoWork();}private int DoWork(){ DoSomeWork; return 1;}
  13. 13. TAPprotected void Page_Load(...) Protected async void Page_Load(...){ { int r = DoWork(); int r = await DoWorkAsync();} }private int DoWork() Private async Task<int> DoWorkAsync(){ { DoSomeWork; await Task.Run(DoSomeWork); return 1; return 1;} }
  14. 14. demoAsynchronize the Application
  15. 15. Thank You
  1. A particular slide catching your eye?

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

×