What's the meaning of "UseTaskFriendlySynchronizationContext"?
asp.netAsynchronousasp.net Problem Overview
There is a new app setting in asp.net 4.5
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
code like this can run in asp.net 4.0
protected void Button1_Click(object sender, EventArgs e)
{
CallAysnc();
}
public void CallAysnc()
{
AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(Guid.NewGuid().ToString());
WebClient client = new WebClient();
client.DownloadStringCompleted += (object sender, DownloadStringCompletedEventArgs e) =>
{
asyncOp.PostOperationCompleted(CallCompleted, e.Result);
};
client.DownloadStringAsync(new Uri("http://www.google.com"));
}
private void CallCompleted(object args)
{
Response.Write(args.ToString());
}
But it doesn't work in asp.net 4.5,and when I remove the new appsetting,it works again!
So what's the meaning of "UseTaskFriendlySynchronizationContext" ?
asp.net Solutions
Solution 1 - asp.net
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
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);
}
Solution 2 - asp.net
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.