I've got an ASP.NET WebForms application which has pages using Async=True
and I'm using RegisterAsyncTask(new PageAsyncTask(InitialiseAsync));
in my OnLoad
method to call business logic asynchronously.
Now I know ASP.NET WebForms requires async calls to be followed with ConfigureAwait(true)
as it needs to return back to the original synchronisation context to finish the page. However the async call chain goes down into a library assembly (also built by us). The library doesn't need to know, nor care about sync contexts to do its asynchronous job. Also, it could (potentially) be used in other contexts such as a Console app.
Therefore:
- should the library methods always use
ConfigureAwait(true)
(in case it's being used by a context sensitive application such as ASP.NET Web Forms)?; or - is it okay for the library methods to use
ConfigureAwait(false)
and for the WebForms app to useConfigureAwait(true)
?; or - (I'm sure this is not the answer but...) should I pass in a boolean value to the library stating whether to use
ConfigureAwait(true)
orConfigureAwait(false)
?
I've been using option 1 up until now but I'm now suspecting that I should be using option 2 so that the library code can await
back onto any thread and the app will eventually context switch back to the required context thread when the call stack comes back out.
Is that right? Thanks