What's the meaning of "UseTaskFriendlySynchronizationContext"?
Asked Answered
J

2

68

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" ?

Joshi answered 5/3, 2012 at 7:6 Comment(0)
A
78

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);
}
Agnola answered 5/3, 2012 at 7:6 Comment(4)
I used the aim code,and it works fine. How to know it is not currently supported? What situation is not support?Joshi
@Dozer, I'm guessing you don't have UseTaskFriendlySynchronizationContext set to true? I get an error about Response not being available (easy to workaround but not if controls you can't change are coded to access it). If you follow that link to the Microsoft Forums you can read about a couple of problems I had and the comment about it not being supported from the Microsoft employee. I got the impression it doesn't automatically plug into RegisterAsyncTask yet due to issues (don't know what, but you can ask in that forum) and if not it then you'll get no benefit over running it synchronously.Agnola
The answer in MSDN said:"In your particular example, having an async void method without setting the <appSettings> switch actually introduces a race condition in your Page, where the DataBind() method is not guaranteed to be called before the Page is actually rendered and its output sent to the client".But I tried it and the page always render after the DataBind() method.Joshi
I found one problem after remove the <appSettings>. If you write two Method: async Page_Load, async Button1_Click.When you click the button,this two method will run parallel. I think this situation is a "race condition".Joshi
B
13

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.

Breaststroke answered 6/7, 2014 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.