简体   繁体   English

ASP.Net vs MVC vs WebAPI和UseTaskFriendlySynchronizationContext

[英]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: 所以这是我的问题:

  1. Does this setting apply to everything ASP.Net or is it specific to the page lifecycle stuff in ASP.Net (which I've not used much) 此设置是否适用于ASP.Net的所有内容,还是特定于ASP.Net中的页面生命周期内容(我没有多少使用)
  2. If it is so critical for modern async programming, why don't any tutorials or templates reference it? 如果它对于现代异步编程至关重要,为什么没有任何教程或模板引用它?
  3. Would using Thread.CurrentPrincipal's claims in a referenced library that uses ConfigureAwait(false) pose any problems or will the flowing of ExecutionContext's logical call context take care of me there? 在使用ConfigureAwait(false)的引用库中使用Thread.CurrentPrincipal的声明会造成任何问题,或者ExecutionContext的逻辑调用上下文的流动会在那里照顾我吗? (my reading and testing so far indicates that it will) (到目前为止我的阅读和测试表明它会)

Here are some of the articles I've seen regarding UseTaskFriendlySynchronizationContext : 以下是我见过的有关UseTaskFriendlySynchronizationContext一些文章:

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 设置targetFramework4.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: 回答您的具体问题:

  1. The setting affects ASP.NET request handling for all kinds of requests, not just WebForms. 该设置会影响对各种请求的ASP.NET请求处理,而不仅仅是WebForms。
  2. Most async tutorials assume a GUI application scenario. 大多数async教程都假设一个GUI应用程序场景。
  3. I'm not sure; 我不确定; I think this would be better as a separate question. 我认为这会作为一个单独的问题更好。 My gut feeling is that you can't depend on Thread.CurrentPrincipal after you leave the ASP.NET context. 我的直觉是你离开ASP.NET上下文后不能依赖Thread.CurrentPrincipal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM