30

When implementing against an interface (because of mocking, remoting or similiar) using the await keyword and having an interface with methods returning Task<> :

interface IFoo
{
    Task<BigInteger> CalculateFaculty(int value);
}

the compiler comes up with an error:

The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'

Which is a bit unusual considering the return type is "Task". This problem is kind of frustrating and forces me to "fall back" using continuation style or providing an extra proxy around this interface (and therefore for almost every interface which is not really feasible for me)

Does anyone have a good idea on how to solve this?

4
  • 2
    what is the code that gives an error? Surely not the interface declaration...
    – jeroenh
    Sep 25, 2012 at 11:13
  • possible duplicate of Interfaces and async methods Aug 8, 2013 at 4:18
  • 1
    Meanwhile I've extended my async/await knowledge "a bit" and the key thing here to understand is: "await" is a compiler tool that asks the compiler to rewrite a method that is written in a "synchronous" into an "asynchronous callback" style. "async" just marks a method for being allowed to contain "await"s (plus deals with the return value). So the keywords don't make sense on a pure method signature (method without body) like in an interface definition as there is nothing for the compiler to rewrite. Again: From a caller's perspective async/await are not relevant. Returning a Task counts. Apr 4, 2016 at 9:34
  • This seems like something they should fix. Why should an interface not explictly specify implementations shouldn't be async? An interface is just a list of signatures. If it impacts how you call a method (which it does in the concrete method), the interface should know about it. Seems like one of those cases when being a purist goes against commonsense.
    – arviman
    Jan 27, 2017 at 8:44

3 Answers 3

32

The message is not about the interface, but about the calling method. You need to mark the method containing the await keyword with the async modifier:

public interface IFoo
{
    Task<int> AwaitableMethod();
}

class Bar
{
    static async Task AsyncMethod() // marked as async!
    {
        IFoo x;
        await x.AwaitableMethod();
    }
}
3
  • 3
    Shame on me. You are totally right and the error states what's wrong quite clearly. I just kept misreading it in the context of the interface method instead of the method calling the interface method. Thank you! Sep 25, 2012 at 11:28
  • 2
    This is not a good example, because Main() can't be async.
    – svick
    Sep 25, 2012 at 12:09
  • Well technically it's just another static method, so it CAN be async (the code I wrote compiled). Of course you're right in that it doesn't make much sense. Updated my answer...
    – jeroenh
    Sep 25, 2012 at 14:38
12

This must be ok:

interface IFoo
{
    Task<BigInteger> CalculateFaculty(int value);
}

public class Foo: IFoo
{
  public async Task<BigInteger> CalculateFaculty(int value)
  {
    var res =  await AsyncCall();
    return res;
  }
}

usage:

 public async Task DoSomething(IFoo foo) 
 { 
   var result = await foo.CalculateFaculty(123); 
 }
2
  • 2
    I am just curious: Wouldn't it be better to rename the method to CalculateFacultyAsync then ? I am new to Async/Await and according to the MSDN the name should indicate the async behaviour.
    – Bjoern
    Jan 17, 2017 at 8:32
  • There are different options around. Jan 17, 2017 at 8:42
-1

The compiler message is not on the usage on the interface. It is an asynchronous request on a task that is implemented by the runtime when an appropriate await is encountered on the operation.

Usage should be on the method implementation ie. the class; the interface usage is the same as it would be for setters and getters in an interface.

Your Answer

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

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