APG301: Asynchronous Programming for ASP.NET Developers

Download Report

Transcript APG301: Asynchronous Programming for ASP.NET Developers

Sofia, Bulgaria | 9-10 October
Asynchronous Programming
for ASP.NET 2.0 Developers
Julie Lerman
The Data Farm
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
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
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
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)
Hello World
WS Proxy class
BeginHelloWorld
http://webservice
EndHelloWorld
Sofia, Bulgaria | 9-10 October
Async Event Pattern
Application Process
Thread A
Sub MethodthatCallsLRP
Call BeginLRP
Do other stuff
Do other stuff
End Sub
External Process
(or separate thread
in app process)
Do long running
process
Thread B
Sub LRPCompleted_Event_Handler
EndLRP(IAsyncResult)
Do stuff with results sent back
End Sub
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
Sofia, Bulgaria | 9-10 October
Coding Async Pages
● Client Side
● <% @Page Async=True .... %>
● Server Side
● Page.AddOnPreRenderCompleteAsync
(new
BeginEventHandler(MyBeginMethod),
new EndEventHandler (MyEndMethod)
)
Sofia, Bulgaria | 9-10 October
Sofia, Bulgaria | 9-10 October
Demonstration
Asynchronous Pages
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
Sofia, Bulgaria | 9-10 October
Sofia, Bulgaria | 9-10 October
Demonstration
Asynchronous Web Services
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
● <@%Page AsyncTimeout =“##”>
● Per task
● Defaults at 45 seconds
Sofia, Bulgaria | 9-10 October
Sofia, Bulgaria | 9-10 October
Demonstration
Asynchronous Tasks
Async Data Binding
● SqlCommand’s new async commands
● BeginExecuteReader/EndExecuteReader
● BeginExecuteXmlReader/EndExecuteXmlReader
● BeginExecuteNonQuery/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
Sofia, Bulgaria | 9-10 October
Sofia, Bulgaria | 9-10 October
Demonstration
Asynchronous Data Binding
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
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
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
Sofia, Bulgaria | 9-10 October
Client Side Callback
Puzzle Pieces
Server Side (code behind)
Client Side
[1] Implement
ICallbackEventHandler
in code or declaratively
[2] Create New Data Function
Implements
RaiseCallbackEvent
[3] Return Data to Client Function
Implements
GetCallbackResult
[4] GetCallbackEventReference
wire up page, display script,
functions and affected control
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
[5] RegisterClientScriptBlock
script that wires things up
on the client side
Sofia, Bulgaria | 9-10 October
Sofia, Bulgaria | 9-10 October
Demonstration
Partial Page Postback with ATLAS
Post-Cache Substitution
● Update pieces page when rest of page is
cached
● <asp:Substitution> 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
Sofia, Bulgaria | 9-10 October
Demonstration
Post-Cache Substitution
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 postback
Sofia, Bulgaria | 9-10 October
Thank You!
● Contact Info
Julia Lerman
The Data Farm
[email protected]
www.thedatafarm.com
● Blog
● www.thedatafarm.com/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 :
http://msdn.microsoft.com/library/default.asp?url=/library/enus/dnvs05/html/async2.asp
Sofia, Bulgaria | 9-10 October
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