Unable to declare Interface " async Task<myObject> MyMethod(Object myObj); "

C#Async AwaitInterfaceasp.net 4.5

C# Problem Overview


I'm unable to declare

interface IMyInterface
{
   async Task<myObject> MyMethod(Object myObj);
}

The compiler tells me:

  • The modifier async isn't valid for this item
  • The async modifier can only be used for methods that have a body

Is this something that should be implemented, or does the nature of async & await prohibit this from ever occurring?

C# Solutions


Solution 1 - C#

> Whether a method is implemented using async/await or not is an > implementation detail. How the method should behave is a contract > detail, which should be specified in the normal way. > > Note that if you make the method return a Task or a Task<T>, it's more > obvious that it's meant to be asynchronous, and will probably be hard > to implement without being asynchronous.

From https://stackoverflow.com/a/6274601/4384

Solution 2 - C#

Whether or not your implementation is async, has no relevance to your interface. In other words, the interface cannot specify that a given method must be implemented in an asynchronous way.

Just take async out of your interface and it will compile; however, there is no way to enforce asynchronous implementation just by specifying an interface.

Solution 3 - C#

If you have an interface with two implementations (one that is truly async and the other that is synchronous) this is what it would look like for each implementation - with both returning a Task<bool>.

public interface IUserManager
{
    Task<bool> IsUserInRole(string roleName);
}

public class UserManager1 : IUserManager
{
    public async Task<bool> IsUserInRole(string roleName)
    {
        return await _userManager.IsInRoleAsync(_profile.Id, roleName);
    }
}

public class UserManager2 : IUserManager
{
    public Task<bool> IsUserInRole(string roleName)
    {
        return Task.FromResult(Roles.IsUserInRole(roleName));
    }
}

If it is a void method you need to return Task.CompletedTask; from the non async method (I think .NET 4.5 and later)

See also : https://stackoverflow.com/questions/19846507/return-taskbool-instantly

Solution 4 - C#

The async modifier is an 'implementation detail', it affects how a method can do stuff asynchronously, not if it does so.

So async has no business being inside an interface.

interface IService { Task DoSomethingAsync(); }

class A : IService { public async Task DoSomethingAsync() { ... } }

class B : IService { public Task DoSomethingAsync() { ... } }

Classes A and B are both perfectly valid. Both methods are awaitable. A consumer of the interface doesn't know or care if they use async.

B.DoSomethingAsync() can be and probably will be asynchronous.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmakerofthings7View Question on Stackoverflow
Solution 1 - C#stuartdView Answer on Stackoverflow
Solution 2 - C#Roy DictusView Answer on Stackoverflow
Solution 3 - C#Simon_WeaverView Answer on Stackoverflow
Solution 4 - C#Henk HoltermanView Answer on Stackoverflow