Asynchronous Programming in ASP.NET

15
Asynchronous Programming in ASP.NET Chris Dufour, ASP .NET MVP Software Architect, Compuware Follow me @chrduf http://www.linkedin.com/in/cdufour

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.

Transcript of Asynchronous Programming in ASP.NET

Page 1: Asynchronous Programming in ASP.NET

Asynchronous Programming in ASP.NETChris Dufour, ASP .NET MVPSoftware Architect, Compuware

Follow me @chrduf

http://www.linkedin.com/in/cdufour

Page 2: Asynchronous Programming in ASP.NET

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

Page 3: Asynchronous Programming in ASP.NET

ASP.NET Page Lifecycle

Page 4: Asynchronous Programming in ASP.NET

demoLoad Test Synchronous Page

Page 5: Asynchronous Programming in ASP.NET

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

Page 6: Asynchronous Programming in ASP.NET

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)

Page 7: Asynchronous Programming in ASP.NET

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)

Page 8: Asynchronous Programming in ASP.NET

ASP.NET Asynchronous Page Lifecycle

Page 9: Asynchronous Programming in ASP.NET

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(); }}

Page 10: Asynchronous Programming in ASP.NET

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

Page 11: Asynchronous Programming in ASP.NET

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)

Page 12: Asynchronous Programming in ASP.NET

TAPprotected void Page_Load(...){ int r = DoWork();}

private int DoWork(){ DoSomeWork; return 1;}

Page 13: Asynchronous Programming in ASP.NET

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;}

Page 14: Asynchronous Programming in ASP.NET

demoAsynchronize the Application

Page 15: Asynchronous Programming in ASP.NET

Thank You