Upload
chris-dufour
View
3.586
Download
0
Tags:
Embed Size (px)
DESCRIPTION
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.
Citation preview
Asynchronous Programming in ASP.NETChris Dufour, ASP .NET MVPSoftware Architect, Compuware
Follow me @chrduf
http://www.linkedin.com/in/cdufour
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
ASP.NET Page Lifecycle
demoLoad Test Synchronous Page
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
Asynchronous History
• 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
.NET 1.1 - APM (Asynchronous Programming Model)
Asynchronous History
• 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
.NET 2.0 - EAP (Event-Based Asynchronous Pattern)
ASP.NET Asynchronous Page Lifecycle
Asynchronous Data Bindingpublic partial class AsyncDataBind : System.Web.UI.Page{ private SqlConnection _connection; private SqlCommand _command; private SqlDataReader _reader;
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Hook PreRenderComplete event for data binding this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
// Register async methods AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginAsyncOperation), new EndEventHandler(EndAsyncOperation) ); } } 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); }
void EndAsyncOperation(IAsyncResult ar) { _reader = _command.EndExecuteReader(ar); }
protected void Page_PreRenderComplete(object sender, EventArgs e) { Output.DataSource = _reader; Output.DataBind(); }
public override void Dispose() { if (_connection != null) _connection.Close(); base.Dispose(); }}
Asynchronous History
• Call XxxAsync to start work• Returns Task (or Task<T>) to reflect in-flight status• Problems with thread affinity
• No second method or event
.NET 4.0 – TPL (Task Parallel Library)
Task<int> t = SomethingAsync(…);//do work, checking t.Statusint r = t.Result
Asynchronous History
• 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>
.NET 4.5 – TAP (Task-Based Asynchronous Pattern)
TAPprotected void Page_Load(...){ int r = DoWork();}
private int DoWork(){ DoSomeWork; return 1;}
TAPprotected void Page_Load(...){ int r = DoWork();}
private int DoWork(){ DoSomeWork; return 1;}
Protected async void Page_Load(...){ int r = await DoWorkAsync();}
Private async Task<int> DoWorkAsync(){ await Task.Run(DoSomeWork); return 1;}
demoAsynchronize the Application
Thank You