[英]ASP.Net vs MVC vs WebAPI and UseTaskFriendlySynchronizationContext
I've got a couple ASP.Net MVC and WebAPI projects. 我有几个ASP.Net MVC和WebAPI项目。 Most of them are up to date (MVC 5 / WebAPI 2).
其中大多数都是最新的(MVC 5 / WebAPI 2)。 I've been double-checking my security assumptions since I'm implementing a global filter (for MVC) and a delegating handler (for WebAPI) to unify security across the system.
我一直在仔细检查我的安全假设,因为我正在实现一个全局过滤器(用于MVC)和一个委托处理程序(用于WebAPI)来统一整个系统的安全性。
In that context, I've come across a few articles and posts (see below) which say you should always be setting UseTaskFriendlySynchronizationContext
to true
(it defaults to false
). 在这种情况下,我遇到了一些文章和帖子(见下文),它们说你应该总是将
UseTaskFriendlySynchronizationContext
设置为true
(默认为false
)。 This seems odd to me as even in VS2013 using MVC 5 and WebAPI 2 new project templates (as well as the ASP.Net WebForms template) do not set this app setting at all. 这对我来说似乎很奇怪,因为即使在VS2013中使用MVC 5和WebAPI 2新项目模板(以及ASP.Net WebForms模板)也根本不设置此应用程序设置。
The MSDN documentation on this setting is practically non-existent and the posts I've found that do say it's required for async programming seem to be in the context of WebForms. 关于此设置的MSDN文档几乎不存在,我发现的帖子确实说它是异步编程所必需的,似乎是在WebForms的上下文中。
So here are my questions: 所以这是我的问题:
Here are some of the articles I've seen regarding UseTaskFriendlySynchronizationContext
: 以下是我见过的有关
UseTaskFriendlySynchronizationContext
一些文章:
SynchronizationContext
in ASP.NET by Marcus van Houdt SynchronizationContext
Some articles that really helped me get a grasp on how all this stuff works that never mention UseTaskFriendlySynchronizationContext
: 一些文章真正帮助我掌握了所有这些东西的工作原理,从未提及
UseTaskFriendlySynchronizationContext
:
The missing key reference is this blog post . 缺少的关键参考是这篇博文 。 Specifically, you need to either set
UseTaskFriendlySynchronizationContext
or set targetFramework
to 4.5
. 具体来说,您需要或者设置
UseTaskFriendlySynchronizationContext
或设置targetFramework
至4.5
。 Creating a new project sets targetFramework
to 4.5
, so you do get the correct behavior ( UseTaskFriendlySynchronizationContext
is implicitly set to true
). 创建新项目
targetFramework
设置为4.5
,因此您可以获得正确的行为( UseTaskFriendlySynchronizationContext
隐式设置为true
)。
To answer your specific questions: 回答您的具体问题:
async
tutorials assume a GUI application scenario. async
教程都假设一个GUI应用程序场景。 Thread.CurrentPrincipal
after you leave the ASP.NET context. Thread.CurrentPrincipal
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.