79

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.

7
  • 21
    Asynchronous code is now so ubiquitous that I don't see it as a practice anymore, unless you really have both versions. Dec 29, 2018 at 20:20
  • 4
    "It's the recommended convention for async methods" is it? It's kind of assumed, now.
    – Alexander
    Dec 30, 2018 at 0:05
  • 2
    What we can do using Visual Studio Text Editor configurations or .editorconfig file, is limited to warn about methods with async modifier, we cannot apply the rule for methods which return Task. Dec 30, 2018 at 0:17
  • 4
    @rory.ap No, I'm suggesting that asynchronous calls are so common now that it doesn't warrant special treatment. You naturally think about checking which classes a method belongs to, what its return type is, etc. Yet we don't annotate method names with what classes they belong to, or what their arg and return types are. I'm saying to give async the same treatment. It's just "one of those things" you check on a function when you learn how to use it, without it being called out explicitly in a name.
    – Alexander
    Dec 30, 2018 at 22:25
  • 2
    Hopefully using ...Async in the name goes the way of Hungarian Notation!
    – Kit
    Apr 3, 2020 at 0:50

3 Answers 3

95

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

6
  • 3
    Despite all these up-votes don't see how it works for me. No suggestions, warning, errors - nothing
    – Kamarey
    Jan 1, 2019 at 9:23
  • 1
    @Kamarey after applying this rule. try closing your editor, reopen it and delete one of your async method without Async postfix and try adding again. It will start working, you may also consider to change severity to Warning. Jan 1, 2019 at 12:26
  • 1
    This won;t catch async methods that don't use the modifier (ie. return Task but don't directly await).
    – Richard
    Mar 5, 2020 at 10:23
  • 3
    My experience: the naming rule will work for a method, except it will not work when the method is declared on an interface. gist.github.com/arthg/c46cb81921f8930d6e4c83858f198897
    – Arthur
    Sep 21, 2020 at 16:47
  • is there a fix for this @Arthur Jul 7, 2021 at 8:38
38

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:

3
  • 1
    I discovered that actually, you don't need to "Add new item", you literally just need to drop the .editorconfig file in the same directory as the .sln file and it is detected.
    – Jez
    Dec 30, 2018 at 21:52
  • 1
    The file location can be even in parent folder of your solution. It's not necessary to have it in solution folder structure. Putting in project structure is just for having it available in VS to edit it. Dec 31, 2018 at 2:44
  • This is pretty neat, the only problem I see is that this also applies to async void event handlers, which I don't want. Is there a way to exclude async void methods?
    – Julian
    Nov 7 at 13:56
2

The Microsoft.VisualStudio.Threading.Analyzers analyzer offers this under the rule VSTHRD200

More info here https://github.com/microsoft/vs-threading/blob/main/doc/analyzers/VSTHRD200.md

tl;dr:

<PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.1.46">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
1
  • I want to thank myself from the past for commenting the above. Also, tl;dr: <PackageReference Include="Microsoft.VisualStudio.Threading.Analyzers" Version="17.1.46"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
    – Amichai
    Apr 5, 2022 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.