What's the meaning of "UseTaskFriendlySynchronizationContext"? What's the meaning of "UseTaskFriendlySynchronizationContext"? asp.net asp.net

What's the meaning of "UseTaskFriendlySynchronizationContext"?


Regarding UseTaskFriendlySynchronizationContext, from Microsoft Forums:

That tells ASP.NET to use an entirely new asynchronous pipeline which follows CLR conventions for kicking off asynchronous operations, including returning threads to the ThreadPool when necessary. ASP.NET 4.0 and below followed its own conventions which went against CLR guidelines, and if the switch is not enabled it is very easy for asynchronous methods to run synchronously, deadlock the request, or otherwise not behave as expected.

Also, I think AsyncOperationManager is intended for desktop applications. For ASP.NET apps you should be using RegisterAsyncTask and setting <%@ Page Async="true", see here for more details.

So using the new c# keywords your example would be:

protected void Button1_Click(object sender, EventArgs e){    RegisterAsyncTask(new PageAsyncTask(CallAysnc));}private async Task CallAysnc(){    var res = await new WebClient().DownloadStringTaskAsync("http://www.google.com");    Response.Write(res);}

The aim is to support the following by release but is not currently supported in the beta:

protected async void Button1_Click(object sender, EventArgs e){    var res = await new WebClient().DownloadStringTaskAsync("http://www.google.com");    Response.Write(res);}


More details, quoted from ASP.NET 4.5.1 documentation for appSettings on MSDN:

aspnet:UseTaskFriendlySynchronizationContext

Specifies how asynchronous code paths in ASP.NET 4.5 behave.

...

If this key value is set to false [default], asynchronous code paths in ASP.NET 4.5 behave as they did in ASP.NET 4.0. If this key value is set to true, ASP.NET 4.5 uses code paths that are optimized for Task-returning APIs. Setting this compatibility switch is mandatory for WebSockets-enabled applications, for using Task-based asynchrony in Web Forms pages, and for certain other asynchronous behaviors.