Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a warning in Visual Studio when async methods don't end in 'Async'?

How can I get Visual Studio to give me a naming warning each time I create an asynchronous method that doesn't end in "Async"?

It's the recommended convention for asynchronous methods, but I often find myself forgetting to add that suffix and a warning would be useful.

like image 663
Jez Avatar asked Dec 29 '18 20:12

Jez


People also ask

What happens if we execute an asynchronous method but don't await it?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

How do I debug async code in Visual Studio?

Double-clicking an active or awaiting task shows the async call stack in the Call Stack window. To understand which thread is running a specific task, you can swap between the Parallel Threads and Parallel Tasks windows. You can do this by right-clicking and selecting Go To Thread in the context menu.

Is it OK to not await async?

If you forget to use await while calling an async function, the function starts executing. This means that await is not required for executing the function. The async function will return a promise, which you can use later.

Can I use async without await C#?

The warning is exactly right: if you mark your method async but don't use await anywhere, then your method won't be asynchronous. If you call it, all the code inside the method will execute synchronously.


2 Answers

From Options,

  • Go to Text EditorBasicCode Style → Naming
  • Select Manage Specifications and Add New Specification
  • Select Method, tick all accessibility options and from Modifiers, select Async.
  • Give the title as Async Method and save
  • Now get into Manage Naming Styles and add New Style. Add Async as suffix and give it a name as AsyncSuffix, and then save
  • Now press the plus sign and create new naming. Select specification as Async Method, select required Style as AsyncPostfix and Severity as Suggestor.
  • Click OK and save

Enter image description here

Enter image description here

Enter image description here

like image 108
Derviş Kayımbaşıoğlu Avatar answered Oct 05 '22 23:10

Derviş Kayımbaşıoğlu


In addition to Visual Studio Text Editor Settings, you can create portable, custom editor settings .editorconfig file. Visual Studio 2017 natively supports .editorconfig files.

By creating the .editorconfig file as part of the repository and pushing it to the repository, you can enforce consistent coding styles for everyone that works in that codebase, regardless of their Visual Studio Text Editor Settings.

The Coding conventions you use on your personal projects may differ from those used on your team's projects. EditorConfig files resolve this problem by enabling you to have a configuration for each scenario.

EditorConfig settings take precedence over global Visual Studio text editor settings.

To do so:

  1. In the Solution Explorer, select the Solution, Project or a folder in the project, depending on the scope which you want to apply the naming rule.

  2. Right click and select Add New Item or Press Ctrl + Shift + A

  3. Choose Text File file from General categories and enter .editorconfig as file name.

Note: The file location can be even in parent folder of your solution. It's not necessary to have it in solution.

Paste the following content in the file:

# Top-most EditorConfig file root = true  [*.{cs,vb}]  # Async methods should have "Async" suffix dotnet_naming_rule.async_methods_end_in_async.symbols = any_async_methods dotnet_naming_rule.async_methods_end_in_async.style = end_in_async dotnet_naming_rule.async_methods_end_in_async.severity = suggestion  dotnet_naming_symbols.any_async_methods.applicable_kinds = method dotnet_naming_symbols.any_async_methods.applicable_accessibilities = * dotnet_naming_symbols.any_async_methods.required_modifiers = async  dotnet_naming_style.end_in_async.required_prefix =  dotnet_naming_style.end_in_async.required_suffix = Async dotnet_naming_style.end_in_async.capitalization = pascal_case dotnet_naming_style.end_in_async.word_separator =  

More Information:

  • Create portable, custom editor settings with EditorConfig
  • EditorConfig
like image 35
Reza Aghaei Avatar answered Oct 06 '22 00:10

Reza Aghaei