2

I have performed a huge refactoring and I converted hundreds of Web API methods that from sync to async.

The problem is that now I have a lot of problems! Mainly because every caller of those methods should use the await keyword in every call.

The compiler has been useful, since most of the cases can be detected an fixed watching compiler errors/warnings, but there are silent cases when the compiler doesn't help.

For instance, in situations like this:

public async Task<Item> Example()
{
    return new Item
    { 
        Property = GetName();
    };
}

private Task<string> GetName() 
{
    return Task.FromResult("Hello");
}

class Item 
{
    public object Property { get; set; }
}

Please, notice that the GetName method isn't awaited, but the compiler won't complain nor emit any warning, because Property is of type object and will accept anything (and that includes a Task)

Is there any way to detect such un-awaited Task? Maybe an extension or a nice trick?

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • 2
    Change `Property` from `object` to `string`. Is there any reason it needs to be `object`? If it must be `object`, at least change it temporarily and fix just the errors related to not awaiting. – madreflection Oct 24 '19 at 22:27
  • 2
    If you want type checking, you need types for the compiler to check against. That means that `public object Property` will need a more specific type. There may be another way, but this is the problem that type-checking is designed to solve. – Ben Oct 24 '19 at 22:29
  • 2
    Either change `object` to `string`, or change the name of the method when you change its return type (e.g. to `GetNameAsync()`) so that the compiler will err out on all the calls you forgot to modify. – John Wu Oct 24 '19 at 23:52
  • The compiler *does* complain that `Example()` doesn't contain any `await` calls and thus will run synchronously. The code example doesn't do anything asynchronously and offers no advantages over just `return "Hello"`. It does add overhead by creating a lot of `Task` objects – Panagiotis Kanavos Oct 25 '19 at 09:32
  • A possible duplicate was raised as an answer, and subsequently deleted. The link is: https://stackoverflow.com/questions/20350397/how-can-i-tell-if-a-c-sharp-method-is-async-await-via-reflection – halfer Oct 27 '19 at 23:31

1 Answers1

0

I have found an extension that definitely has helped me:

It's available here: Missing await warning

Git-Hub: https://github.com/ykoksen/unused-task-warning

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • The problem is the code itself, there's no need for extensions. You gain nothing by using `return Task.FromResult("Hello");` - you actually add overhead by creating the completed task. The compiler already emits a warning that `Example` doesn't contain `await` and will run synchronously – Panagiotis Kanavos Oct 25 '19 at 09:30
  • In any case, the immediate problem is caused by `object` - use the correct type - `string` or `Task` instead. – Panagiotis Kanavos Oct 25 '19 at 09:35
  • The problem IS the code, of course, but I need something to spot these problems due to the big refactoring I did. When I changed like 200 methods' return types from from T to Task and from void to Task, the callers should await them. I need to be sure that nothing is left un-awaited. That's why I asked :) – SuperJMN Oct 25 '19 at 11:35