1

I have many legacy async tests that have no ConfigureAwait(false) ending. I use search utility in Visual Studio 2019. My regex is: await .+(?!ConfigureAwait\(false\);) But it finds code that starts with "await" and also has "ConfigureAwait(false)". I want it to show me only the lines that have "async", but don't have "ConfigureAwait(false))";

Aristos
  • 66,005
  • 16
  • 114
  • 150
Nick Resh
  • 13
  • 2
  • Use `await(?!.*ConfigureAwait\(false\);$) .+` or `await(?!.*ConfigureAwait\(false\);\r?$) .+` – Wiktor Stribiżew Apr 28 '21 at 21:57
  • Your visual studio doesn't raise an info/warning about this already? – Caius Jard Apr 28 '21 at 22:02
  • No, it doesn't. Tried many time - it works by matching everything in the search bar without negative lookahead – Nick Resh Apr 28 '21 at 22:05
  • 1
    Install Roslynator code analyzer https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1090.md - you can filter the messages to just the error, and maybe even you can click the lightbulb and fix all in solution (vs will add the call for you) – Caius Jard Apr 28 '21 at 22:07
  • Surely you would want `.ConfigureAwait(false)` in _library_ code if anything, more than in _"tests"_ which I assume you mean _unit tests?"_ –  Apr 28 '21 at 22:28
  • Why do you need `ConfigureAwait(false)` in the tests? – Julian Apr 28 '21 at 22:29
  • I need it cause my team-lead decided so, I've got to stick to his guide – Nick Resh Apr 28 '21 at 23:24
  • Adding `.ConfigureAwait(false)` willy-nilly might not be beneficial. In ASP.NET Core for example it does nothing. _["ConfigureAwait **only has effects on code running in the context of a SynchronizationContext** which ASP.NET Core doesn't have"](https://stackoverflow.com/a/42058544/585968)_. There might be other situations where it might not prove useful –  Apr 28 '21 at 23:28

1 Answers1

2

A better solution would be to install "SonarLint" (if you're on visual studio, for other IDEs you need to check for availability). Once it analyzes your code, you can easily filter out 'await' calls lacking 'ConfigureAwait' (if memory serves me right, they will be categorized as "Major Issue").

  • If you use Resharper (or Rider IDE) then you can Alt-Tab to select which type of ConfigureAwait your project uses (Library or UI). Once this is set, you are only one Alt-Tab away from setting one-statement (or up to all of them) with ConfigureAwait(false) – D. Squire Jan 06 '22 at 09:22