Asynchronous Programming in ASP.NET


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
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Asynchronous Programming in ASP.NET

  1. 1. Asynchronous Programmingin ASP.NETChris Dufour, ASP .NET MVPSoftware Architect, Compuware Follow me @chrduf
  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.