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.
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
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. 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. 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
9. Asynchronous Data Binding
public 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. 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 event
Task<int> t = SomethingAsync(…);
//do work, checking t.Status
int r = t.Result
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>