什么是的&QUOT意义; UseTaskFriendlySynchronizationContext"? [英] What's the meaning of "UseTaskFriendlySynchronizationContext"?

查看:117
本文介绍了什么是的&QUOT意义; UseTaskFriendlySynchronizationContext"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有是在asp.net中一个新的应用程序设置4.5

There is a new app setting in asp.net 4.5

<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />

code这样才能在asp.net 4.0中运行。

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());
}

不过,这并不在asp.net 4.5工作,当我删除新appsetting,它再次工作!

But it doesn't work in asp.net 4.5,and when I remove the new appsetting,it works again!

那么,什么是UseTaskFriendlySynchronizationContext?

So what's the meaning of "UseTaskFriendlySynchronizationContext" ?

推荐答案

关于 UseTaskFriendlySynchronizationContext ,从<一个href=\"http://social.msdn.microsoft.com/Forums/en-NZ/async/thread/b2e8c51e-2808-46d0-92e9-b825321d0af8\">Microsoft论坛:

这告诉ASP.NET使用一个全新的异步管线哪些
  遵循CLR约定开球异步操作,
  包括返回线程的线程池在必要的时候。 ASP.NET
  4.0及以下遵循其自身的约定,违背CLR指引,如果没有启用该开关是
  的非常的方便异步方法同步运行,死锁的要求,或以其他方式不按预期的行为。

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.

另外,我觉得 AsyncOperationManager 适用于桌面应用程序。对于ASP.NET应用程式,你应该使用 RegisterAsyncTask 并设置&LT;%@页面异步=真正的,< A HREF =htt​​p://msdn.microsoft.com/en-us/magazine/cc163725.aspx>在这里看到的更多详细信息。

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.

因此​​,使用新的C#关键字的例子是:

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);
}

这篇关于什么是的&QUOT意义; UseTaskFriendlySynchronizationContext&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆