加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core-mvc – 我是否需要在Visual Studio 2017 ASP.NET C

发布时间:2020-12-16 06:55:25 所属栏目:asp.Net 来源:网络整理
导读:我刚刚将Visual Studio 2015 ASP.NET MVC Core项目转换为Visual Studio 2017 …我在错误列表中收到以下信息性消息 消息IDE1006命名规则违规:缺少后缀:’异步’ 此消息出现在我的控制器中,重点关注以下内容: public async TaskIActionResult Index() 这也
我刚刚将Visual Studio 2015 ASP.NET MVC Core项目转换为Visual Studio 2017 …我在错误列表中收到以下信息性消息

消息IDE1006命名规则违规:缺少后缀:’异步’

此消息出现在我的控制器中,重点关注以下内容:

public async Task<IActionResult> Index()

这也适用于创建,删除,详细信息和编辑.消息显示为信息,并适用于我的项目中超过1,000次出现.看来我需要将Index更改为IndexAsync
即.
????改变自:

public async Task<IActionResult> Index()
public async Task<IActionResult> Create()
public async Task<IActionResult> Delete(int? id)
public async Task<IActionResult> Details(int? id)

改成:

public async Task<IActionResult> IndexAsync()
public async Task<IActionResult> CreateAsync()
public async Task<IActionResult> DeleteAsync(int? id)
public async Task<IActionResult> DetailsAysnc(int? id)

这似乎是可选的,因为我的项目将构建并且它不是VS 2015中的问题.我不介意做这项工作,我需要确认在Visual Studio 2017 ASP.NET Core中更改它是正确的方法.

解决方法

微软正在推动你使用async这个词来为你的异步方法添加后缀.为什么? Visual Studio 2017的 release notes提到了这个花絮.

Task-like return types for async methods: This introduces the ability
to return any task-like type from an async method. Previously these
return types were constrained to Task<T> and Task.

听起来不太明显哪些方法只是检查它们的返回类型是异步的.将它们与异步进行后缀可能是一个好主意.在VS提出这个“建议”之前,有一个previous stack overflow question辩论大会.来自微软的Stephen Toub对此发表了讲话.

If a public method is Task-returning and is asynchronous in nature (as
opposed to a method that is known to always execute synchronously to
completion but still returns a Task for some reason),it should have
an “Async” suffix. That’s the guideline. The primary goal here with
the naming is to make it very obvious to a consumer of the
functionality that the method being invoked will likely not complete
all of its work synchronously; it of course also helps with the case
where functionality is exposed with both synchronous and asynchronous
methods such that you need a name difference to distinguish them. How
the method achieves its asynchronous implementation is immaterial to
the naming: whether async/await is used to garner the compiler’s help,
or whether types and methods from System.Threading.Tasks are used
directly (e.g. TaskCompletionSource) doesn’t really matter,as that
doesn’t affect the method’s signature as far as a consumer of the
method is concerned.

Of course,there are always exceptions to a guideline. The most
notable one in the case of naming would be cases where an entire
type’s raison d’etre is to provide async-focused functionality,in
which case having Async on every method would be overkill,e.g. the
methods on Task itself that produce other Tasks.

As for void-returning asynchronous methods,it’s not desirable to have
those in public surface area,since the caller has no good way of
knowing when the asynchronous work has completed. If you must expose a
void-returning asynchronous method publicly,though,you likely do
want to have a name that conveys that asynchronous work is being
initiated,and you could use the “Async” suffix here if it made sense.
Given how rare this case should be,I’d argue it’s really a
case-by-case kind of decision.

I hope that helps,Steve

底线,这是信息性的.但是,随着Microsoft将返回类型扩展到Task以外,它开始变得越来越像最佳实践.用你自己的判断.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读