Upload
felix-riley
View
227
Download
0
Embed Size (px)
Citation preview
Sofia, Bulgaria | 9-10 OctoberSofia, Bulgaria | 9-10 October
Asynchronous Programming for ASP.NET 2.0 Developers
Asynchronous Programming for ASP.NET 2.0 Developers
Julie Lerman
The Data Farm
Julie Lerman
The Data Farm
Sofia, Bulgaria | 9-10 October
About MeAbout 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
OverviewOverview
● 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 .NETAsynchronous 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 MethodsAsynchronous 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 classBeginHelloWorld
EndHelloWorld
http://webservice
Sofia, Bulgaria | 9-10 October
Thread B
Async Event PatternAsync Event Pattern
Application Process
Do long running process
Sub MethodthatCallsLRP
End Sub
Sub LRPCompleted_Event_Handler
End Sub
Thread A
Call BeginLRPDo 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 PagesSynchronous PagesApplicationThread Pool
One thread pool per process
Default 25 threads per pool
Change with SetMaxThreads
??
??
Sofia, Bulgaria | 9-10 October
Asynchronous PagesAsynchronous Pages● Purpose: Handle more requests
● Free up threads when external processes are called1. 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 called1. 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 PagesCoding Async Pages
● Client Side
● <% @Page Async=True .... %>
● Server Side
● Page.AddOnPreRenderCompleteAsync(new BeginEventHandler(MyBeginMethod), new EndEventHandler (MyEndMethod) )
● Client Side
● <% @Page Async=True .... %>
● Server Side
● Page.AddOnPreRenderCompleteAsync(new BeginEventHandler(MyBeginMethod), new EndEventHandler (MyEndMethod) )
Sofia, Bulgaria | 9-10 OctoberSofia, Bulgaria | 9-10 October
DemonstrationDemonstration
Asynchronous PagesAsynchronous Pages
Sofia, Bulgaria | 9-10 October
Async Web ServicesAsync 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 OctoberSofia, Bulgaria | 9-10 October
DemonstrationDemonstration
Asynchronous Web ServicesAsynchronous Web Services
Sofia, Bulgaria | 9-10 October
Asynchronous TasksAsynchronous 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
● (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 OctoberSofia, Bulgaria | 9-10 October
DemonstrationDemonstration
Asynchronous Tasks Asynchronous Tasks
Sofia, Bulgaria | 9-10 October
Async Data BindingAsync 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
● 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 ExecuteBegin/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 OctoberSofia, Bulgaria | 9-10 October
DemonstrationDemonstration
Asynchronous Data Binding Asynchronous Data Binding
Sofia, Bulgaria | 9-10 October
Client-Side CallbacksClient-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 CallbacksClient-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 CallbacksClient-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 PiecesClient Side Callback Puzzle Pieces
[2] Create New Data FunctionImplements
RaiseCallbackEvent
[3] Return Data to Client FunctionImplements
GetCallbackResult
[5] RegisterClientScriptBlockscript 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 Scriptupdate control’s display
upon callback
Triggercalls script [5] registered
from the server side
[1] Implement ICallbackEventHandlerin code or declaratively
Sofia, Bulgaria | 9-10 OctoberSofia, Bulgaria | 9-10 October
DemonstrationDemonstration
Partial Page Postback with ATLASPartial Page Postback with ATLAS
Sofia, Bulgaria | 9-10 October
Post-Cache SubstitutionPost-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
● 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 OctoberSofia, Bulgaria | 9-10 October
DemonstrationDemonstration
Post-Cache SubstitutionPost-Cache Substitution
Sofia, Bulgaria | 9-10 October
SummarySummary
● 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!Thank You!● Contact Info
Julia Lerman
The Data Farm
www.thedatafarm.com
● Blog● www.thedatafarm.com/blog
● Contact InfoJulia Lerman
The Data Farm
www.thedatafarm.com
● Blog● www.thedatafarm.com/blog
Sofia, Bulgaria | 9-10 October
Further ReadingFurther 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/en-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 : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/async2.asp
Sofia, Bulgaria | 9-10 OctoberSofia, 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
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!Thank you!