gpt4 book ai didi

asp.net-core-mvc - 我是否需要在 Visual Studio 2017 ASP.NET Core MVC 中将异步添加到我的 Controller 操作中

转载 作者:行者123 更新时间:2023-12-04 19:35:01 25 4
gpt4 key购买 nike

我刚刚将我的 Visual Studio 2015 ASP.NET MVC Core 项目转换为 Visual Studio 2017 ......我在我的错误列表中收到以下信息性消息

消息 IDE1006 命名规则违规:缺少后缀:'Async'

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

public async Task<IActionResult> Index()

这也适用于创建、删除、详细信息和编辑。这些消息显示为 Informational,适用于我的项目中超过 1,000 次出现。看来我需要将 Index 更改为 IndexAsync
IE。
更改自:
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)

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

最佳答案

Microsoft 正在鼓励您为您的异步方法添加后缀 async。为什么? release notes对于 Visual Studio 2017,请提及此花絮。

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.



听起来,仅通过检查它们的返回类型,哪些方法是异步的会变得不那么明显。为它们添加 async 后缀可能是个好主意。在 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 之外,它开始看起来越来越像最佳实践。使用你自己的判断。

关于asp.net-core-mvc - 我是否需要在 Visual Studio 2017 ASP.NET Core MVC 中将异步添加到我的 Controller 操作中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40694529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com