Sofia, Bulgaria | 9-10 October Asynchronous Programming for ASP.NET 2.0 Developers Julie Lerman The Data Farm Julie Lerman The Data Farm
Sofia, Bulgaria | 9-10 October About Me ●.NET Mentor and Software Consultant ●20+ years developing ●Microsoft.NET MVP, ASPInsider ●INETA Speaker ●Various publications & conferences ●Blogs: thedatafarm.com/blog, blog.ziffdavis.com/devlife ●Founder and leader of Vermont.NET ●Former INETA Board Member ●INETA Speaker Committee ●Vermont Software Developer Alliance Board ●.NET Mentor and Software Consultant ●20+ years developing ●Microsoft.NET MVP, ASPInsider ●INETA Speaker ●Various publications & conferences ●Blogs: thedatafarm.com/blog, blog.ziffdavis.com/devlife ●Founder and leader of Vermont.NET ●Former INETA Board Member ●INETA Speaker Committee ●Vermont Software Developer Alliance Board
Sofia, Bulgaria | 9-10 October Overview ●Asynchronous Patterns in.NET ●New ASP.NET 2.0 Features ●Asynchronous Pages ●Asynchronous Web Services ●Asynchronous Tasks ●Asynchronous Data Access ●Client Side Callbacks ATLAS ●Post-Cache Substitution ●Asynchronous Patterns in.NET ●New ASP.NET 2.0 Features ●Asynchronous Pages ●Asynchronous Web Services ●Asynchronous Tasks ●Asynchronous Data Access ●Client Side Callbacks ATLAS ●Post-Cache Substitution
Sofia, Bulgaria | 9-10 October Asynchronous Patterns in.NET ●Pre-.NET 2.0 ●Delegates & Callbacks ●IAsyncResult ●WaitHandle ●.NET 2.0 Event Based Async Pattern ●Plumbing is hidden ●Still need IAsyncResult in a few cases ●Implemented throughout.NET ●In WinForms, In ASP.NET, In ADO.NET ●Pre-.NET 2.0 ●Delegates & Callbacks ●IAsyncResult ●WaitHandle ●.NET 2.0 Event Based Async Pattern ●Plumbing is hidden ●Still need IAsyncResult in a few cases ●Implemented throughout.NET ●In WinForms, In ASP.NET, In ADO.NET
Sofia, Bulgaria | 9-10 October Asynchronous Methods ●When calling out to external processes ●Use classes that can do so asynchronously ●Class must have ●Begin_Method(callback,state) as IAsyncResult ●End_Method(IAsyncResult) ●When calling out to external processes ●Use classes that can do so asynchronously ●Class must have ●Begin_Method(callback,state) as IAsyncResult ●End_Method(IAsyncResult) Hello World WS Proxy class BeginHelloWorld EndHelloWorld
Sofia, Bulgaria | 9-10 October Thread B Async Event Pattern Application Process Do long running process Sub MethodthatCallsLRP End Sub Sub LRPCompleted_Event_Handler End Sub Thread A Call BeginLRP Do other stuff EndLRP(IAsyncResult) Do stuff with results sent back Do other stuff External Process (or separate thread in app process)
Sofia, Bulgaria | 9-10 October Synchronous Pages Application Thread Pool One thread pool per process Default 25 threads per pool Change with SetMaxThreads ? ? ? ?
Sofia, Bulgaria | 9-10 October Asynchronous Pages ●Purpose: Handle more requests ●Free up threads when external processes are called 1.Begin page rendering 2.Drop thread while async process is running Thread is now available to other requests 3.Grab new thread when process completes 4.Finish rendering ●Purpose: Handle more requests ●Free up threads when external processes are called 1.Begin page rendering 2.Drop thread while async process is running Thread is now available to other requests 3.Grab new thread when process completes 4.Finish rendering
Sofia, Bulgaria | 9-10 October Coding Async Pages ●Client Side ● ●Server Side ●Page.AddOnPreRenderCompleteAsync (new BeginEventHandler(MyBeginMethod), new EndEventHandler (MyEndMethod) ) ●Client Side ● ●Server Side ●Page.AddOnPreRenderCompleteAsync (new BeginEventHandler(MyBeginMethod), new EndEventHandler (MyEndMethod) )
Sofia, Bulgaria | 9-10 October Demonstration Asynchronous Pages
Sofia, Bulgaria | 9-10 October Async Web Services ●IAsyncResult pattern (.NET 1.x & 2.0) ●ws.BeginmyMethod / ws.EndmyMethod ●Manually manage context information ●Coordination ●Use waitHandle to postpone page rendering ●Can be combined with Asynchronous Pages ●Event Based Asynchronous Pattern (NET 2.0) ●ws.myMethodAsync / ws.myMethodCompleted ●HttpContext, Impersonation & culture carry ●Coordination ●Render automatically continues when all services complete ●IAsyncResult pattern (.NET 1.x & 2.0) ●ws.BeginmyMethod / ws.EndmyMethod ●Manually manage context information ●Coordination ●Use waitHandle to postpone page rendering ●Can be combined with Asynchronous Pages ●Event Based Asynchronous Pattern (NET 2.0) ●ws.myMethodAsync / ws.myMethodCompleted ●HttpContext, Impersonation & culture carry ●Coordination ●Render automatically continues when all services complete
Sofia, Bulgaria | 9-10 October Demonstration Asynchronous Web Services
Sofia, Bulgaria | 9-10 October Asynchronous Tasks ●(Page) AddOnPreRenderCompleteAsync ●Limited to one asynchronous call ●Asynchronous Tasks ●Make multiple (non web service) async calls ●Timeout setting ●Completion of multiple tasks is coordinated ●Code ● PageAsyncTask + RegisterAsyncTask ● ●Per task ●Defaults at 45 seconds ●(Page) AddOnPreRenderCompleteAsync ●Limited to one asynchronous call ●Asynchronous Tasks ●Make multiple (non web service) async calls ●Timeout setting ●Completion of multiple tasks is coordinated ●Code ● PageAsyncTask + RegisterAsyncTask ● ●Per task ●Defaults at 45 seconds
Sofia, Bulgaria | 9-10 October Demonstration Asynchronous Tasks
Sofia, Bulgaria | 9-10 October Async Data Binding ●SqlCommand’s new async commands ●BeginExecuteReader/EndExecuteReader ●Begin ExecuteXmlReader/EndExecuteXmlReader ●Begin ExecuteNonQuery/EndExecuteNonQuery ●Call a BeginExecute in MyBeginMethod ●Call an EndExecute in MyEndMethod ●Returns an IAsyncResult ●Cast result back to data object ●DataBind in PreRenderComplete ●“Asynchronous Processing=True” in connection string ●SqlCommand’s new async commands ●BeginExecuteReader/EndExecuteReader ●Begin ExecuteXmlReader/EndExecuteXmlReader ●Begin ExecuteNonQuery/EndExecuteNonQuery ●Call a BeginExecute in MyBeginMethod ●Call an EndExecute in MyEndMethod ●Returns an IAsyncResult ●Cast result back to data object ●DataBind in PreRenderComplete ●“Asynchronous Processing=True” in connection string
Sofia, Bulgaria | 9-10 October Begin/End Execute ●Variety of overloads including callbacks, object state and command behavior ●Must call corresponding End to finish ●End will block SqlCommand until execution completes ●Use separate SqlCommand objects for each asynchronous call ●Variety of overloads including callbacks, object state and command behavior ●Must call corresponding End to finish ●End will block SqlCommand until execution completes ●Use separate SqlCommand objects for each asynchronous call
Sofia, Bulgaria | 9-10 October Demonstration Asynchronous Data Binding
Sofia, Bulgaria | 9-10 October Client-Side Callbacks ●Enables partial page postbacks ●Update controls from server ●Created before AJAX and ATLAS ●Complicated and hard to use ●Recommend AJAX or ATLAS instead ●Enables partial page postbacks ●Update controls from server ●Created before AJAX and ATLAS ●Complicated and hard to use ●Recommend AJAX or ATLAS instead
Sofia, Bulgaria | 9-10 October Client-Side Callbacks ●Calls server side process to update single web server control on a page ●Page does not post back ●Wired up in the code behind ●GetCallBackEventReference ●Ties server method, client script and control ●Four Overloads ●Calls server side process to update single web server control on a page ●Page does not post back ●Wired up in the code behind ●GetCallBackEventReference ●Ties server method, client script and control ●Four Overloads
Sofia, Bulgaria | 9-10 October Client-Side Callbacks ●Avoid using viewstate on affected control ●Browser basics ●Internet Explorer 5+ ●Uses Microsoft.XMLHttp ●Other Browsers ●Uses XMLHttpRequest of the browser’s DOM ●Great for simple scenarios ●AJAX &Atlas for more complex use ●Avoid using viewstate on affected control ●Browser basics ●Internet Explorer 5+ ●Uses Microsoft.XMLHttp ●Other Browsers ●Uses XMLHttpRequest of the browser’s DOM ●Great for simple scenarios ●AJAX &Atlas for more complex use
Sofia, Bulgaria | 9-10 October Client Side Callback Puzzle Pieces [2] Create New Data Function Implements RaiseCallbackEvent [3] Return Data to Client Function Implements GetCallbackResult [5] RegisterClientScriptBlock script that wires things up on the client side Server Side (code behind) [4] GetCallbackEventReference wire up page, display script, functions and affected control Client Side Control (if web server control, avoid ViewState) Display Update Script update control’s display upon callback Trigger calls script [5] registered from the server side [1] Implement ICallbackEventHandler in code or declaratively
Sofia, Bulgaria | 9-10 October Demonstration Partial Page Postback with ATLAS
Sofia, Bulgaria | 9-10 October Post-Cache Substitution ●Update pieces page when rest of page is cached ● control ●Property: MethodName=“myStaticMethod” ●Static method returns a string to control ●Response.WriteSubstitution(....) ●Can be embedded in html ●Can be used elsewhere in code ●Static methods not required when outside page ●Controls are updated after cached page is rendered ●Update pieces page when rest of page is cached ● control ●Property: MethodName=“myStaticMethod” ●Static method returns a string to control ●Response.WriteSubstitution(....) ●Can be embedded in html ●Can be used elsewhere in code ●Static methods not required when outside page ●Controls are updated after cached page is rendered
Sofia, Bulgaria | 9-10 October Demonstration Post-Cache Substitution
Sofia, Bulgaria | 9-10 October Summary ●Build more efficient & responsive web apps with async ASP.NET 2.0 ●Free up threads in the thread pool while page is pre-rendering ●Run multiple processes concurrently to speed up pre-rendering ●Leverage caching when only a bit of the page needs to be changed at postback ●Modify page parts without a full post- back ●Build more efficient & responsive web apps with async ASP.NET 2.0 ●Free up threads in the thread pool while page is pre-rendering ●Run multiple processes concurrently to speed up pre-rendering ●Leverage caching when only a bit of the page needs to be changed at postback ●Modify page parts without a full post- back
Sofia, Bulgaria | 9-10 October Thank You! ●Contact Info Julia Lerman The Data Farm ●Blog ● ●Contact Info Julia Lerman The Data Farm ●Blog ●
Sofia, Bulgaria | 9-10 October Further Reading ●Asynchronous Pages in ASP.NET 2.0, Jeff Prosise, MSDN Magazine Oct 2005 ●Five Undiscovered Features on ASP.NET 2.0, Jeff Prosise Feb 2005 ●Calling Web Services Asynchronously, Dan Wahlin, asp.netPRO, July 2006 ●Fritz Onion Weblog: pluralsight.com/blogs/fritz ●Asynchronous Web Parts, Fritz Onion, MSDN Mag July 2006 ●MSDN VS2005 Docs: “Threads & Threading” ●Asynchronous Command Execution in ADO.NET 2.0, Pablo Castro, Microsoft : us/dnvs05/html/async2.asp ●Asynchronous Pages in ASP.NET 2.0, Jeff Prosise, MSDN Magazine Oct 2005 ●Five Undiscovered Features on ASP.NET 2.0, Jeff Prosise Feb 2005 ●Calling Web Services Asynchronously, Dan Wahlin, asp.netPRO, July 2006 ●Fritz Onion Weblog: pluralsight.com/blogs/fritz ●Asynchronous Web Parts, Fritz Onion, MSDN Mag July 2006 ●MSDN VS2005 Docs: “Threads & Threading” ●Asynchronous Command Execution in ADO.NET 2.0, Pablo Castro, Microsoft : us/dnvs05/html/async2.asp
Sofia, Bulgaria | 9-10 October Please fill out the survey forms! They are the key to amazing prizes that you can get at the end of each day Thank you!
Sofia, Bulgaria | 9-10 October