asp.net - Async/Await in WebForms - How does the continuation run before the page lifecycle ends? -
i've been testing async in webforms , once, question isn't how something, it's how works, work. test code:
protected override void onprerender(eventargs e) { response.write("onprerender<br>"); } protected override void onprerendercomplete(eventargs e) { response.write("onprerendercomplete<br>"); } protected async override void onloadcomplete(eventargs e) { response.write("onloadcomplete<br>"); var t1 = task.factory.startnew(() => { system.threading.thread.sleep(2000); homecoming 1; }); //this run: response.write((await t1).tostring()); }
so task pauses bit , writes out result. question - wouldn't expect work because command has been yielded onloadcomplete method - expect page finish rendering , returned client before task ever returned.
the actual output is:
onloadcomplete onprerender 1onprerendercomplete
so it's clear onloadcomplete method yielded command onprerender run , command returned onloadcomplete. expected result "1" never print because subsequent events fire , page's thread either killed or post-task write happen after response had been sent. guess, given above, it's not surprising if delay 10 seconds, result same.
i assume there's wiring in webform engine ensures awaitables completed before next phase of page's life cycle proceeded. know sure how happens? i'm afraid utilize async/await in methods need finish before other events fear continuation late, if it's handled internally, won't worry.
for asp.net, should utilize async
methods on .net 4.5. i'll explain why @ end.
i have an article on synchronizationcontext
helps fill in blanks on how works on asp.net. first, note asp.net supported asynchronous operations long time ago (.net 2.0 iirc). register asynchronous operations few different ways, description we'll focus on synchronizationcontext.operationstarted
.
asp.net creates synchronizationcontext
each request, , knows request not finish until registered operations have completed (by calling synchronizationcontext.operationcompleted
). event-based asynchronous pattern components (such backgroundworker
) notify synchronizationcontext
automatically when start , complete.
similarly, async void
methods (the new task-based asynchronous pattern) notify synchronizationcontext
automatically when start , complete. when override onloadcomplete
async void
method, compiler inserts code phone call operationstarted
@ origin , operationcompleted
when completes.
so far - asp.net knows maintain request live until asynchronous operations request have completed. true if there no threads processing request.
now caveat: asp.net before .net 4.5 handle @ request level. in asp.net 4.5, lifecycle pipeline made smarter delay page lifecycle until asynchronous operations completed. old asp.net, "pre" handlers start @ point in pipeline may not finish until later. new asp.net delay rest of page execution ensure async
handlers finish before moving on in lifecycle.
also, asp.net 4.5 observe if you've used async
handler there shouldn't one, , notify of error.
asp.net webforms async-await
No comments:
Post a Comment