Find expected methods in code with Visual Studio

I have a problem where methods async

are called in code without await

before it. Is there a way to find all the expected methods that don't await

?

Edit . I'm particularly concerned about a scenario where multiple asynchronization methods are called (ignoring return values), but only one has await

, which is enough for Visual Studio to not warn about it.

+3


source to share


1 answer


If you are using ReSharper and include analysis of the whole solution, your methods that return tasks that are not expected will have the method signature portion of the task greyed out due to the "return value not being used". The danger here is that it will only find methods that are not waiting anywhere in your solution; the warning will go away after one or more usages are updated to wait (or use / reference to Task

).

If you're looking for async methods that don't have a pending call (which means they don't need to be marked async), ReSharper will tell you this in a different way, too.

    class AClass
    {
        public async void Foo() //async grayed out
        {
            DoSomethingAsync();
            Console.WriteLine("Done");
        }

        public Task<bool> DoSomethingAsync() //Task<bool> grayed out
        {
            return Task.Run(() => true);
        }    
    }

      



Note that this will not work if you have code that looks like this:

    class AClass
    {
        public async void Foo()
        {
            bool b = DoSomethingAsync().Result;
            Console.WriteLine("Done");
        }

        public Task<bool> DoSomethingAsync()
        {
            return Task.Run(() => true);
        }    
    }

      

The keyword async

, if present, will still be flagged, which means you can probably figure out pretty quickly that the task is not expected, but if the caller is not marked async, you're out of luck.

0


source







All Articles