Making interface implementations async

C#AsynchronousAsync Await

C# Problem Overview


I’m currently trying to make my application using some Async methods. All my IO is done through explicit implementations of an interface and I am a bit confused about how to make the operations async.

As I see things I have two options in the implementation:

interface IIO
{
    void DoOperation();
}

OPTION1: Do an implicit implementation async and await the result in the implicit implementation.

class IOImplementation : IIO
{

     async void DoOperation()
    {
        await Task.Factory.StartNew(() =>
            {
                //WRITING A FILE OR SOME SUCH THINGAMAGIG
            });
    }

    #region IIO Members

    void IIO.DoOperation()
    {
        DoOperation();
    }

    #endregion
}

OPTION2: Do the explicit implementation async and await the task from the implicit implementation.

class IOAsyncImplementation : IIO
{
    private Task DoOperationAsync()
    {
        return new Task(() =>
            {
                //DO ALL THE HEAVY LIFTING!!!
            });
    }

    #region IIOAsync Members

    async void IIO.DoOperation()
    {
        await DoOperationAsync();
    }

    #endregion
}

Are one of these implementations better than the other or is there another way to go that I am not thinking of?

C# Solutions


Solution 1 - C#

Neither of these options is correct. You're trying to implement a synchronous interface asynchronously. Don't do that. The problem is that when DoOperation() returns, the operation won't be complete yet. Worse, if an exception happens during the operation (which is very common with IO operations), the user won't have a chance to deal with that exception.

What you need to do is to modify the interface, so that it is asynchronous:

interface IIO
{
    Task DoOperationAsync(); // note: no async here
}

class IOImplementation : IIO
{
    public async Task DoOperationAsync()
    {
        // perform the operation here
    }
}

This way, the user will see that the operation is async and they will be able to await it. This also pretty much forces the users of your code to switch to async, but that's unavoidable.

Also, I assume using StartNew() in your implementation is just an example, you shouldn't need that to implement asynchronous IO. (And new Task() is even worse, that won't even work, because you don't Start() the Task.)

Solution 2 - C#

Better solution is to introduce another interface for async operations. New interface must inherit from original interface.

Example:

interface IIO
{
    void DoOperation();
}

interface IIOAsync : IIO
{
    Task DoOperationAsync();
}


class ClsAsync : IIOAsync
{
    public void DoOperation()
    {
        DoOperationAsync().GetAwaiter().GetResult();
    }

    public async Task DoOperationAsync()
    {
        //just an async code demo
        await Task.Delay(1000);
    }
}


class Program
{
    static void Main(string[] args)
    {
        IIOAsync asAsync = new ClsAsync();
        IIO asSync = asAsync;

        Console.WriteLine(DateTime.Now.Second);

        asAsync.DoOperation();
        Console.WriteLine("After call to sync func using Async iface: {0}", 
            DateTime.Now.Second);

        asAsync.DoOperationAsync().GetAwaiter().GetResult();
        Console.WriteLine("After call to async func using Async iface: {0}", 
            DateTime.Now.Second);

        asSync.DoOperation();
        Console.WriteLine("After call to sync func using Sync iface: {0}", 
            DateTime.Now.Second);

        Console.ReadKey(true);
    }
}

P.S. Redesign your async operations so they return Task instead of void, unless you really must return void.

Solution 3 - C#

I created a sample app based on Svick's answer and found that calling IOImplementation.DoOperationAsync() without the async keyword does not result in a compiler/Visual Studio warning. This was based on Visual Studio 2019 and .NET Core 3.1.

Sample code below.

public interface ISomething
{
	Task DoSomethingAsync();
}
public class Something : ISomething
{
	public async Task DoSomethingAsync()
	{
		await Task.Run(() => Thread.Sleep(2000));
		Console.WriteLine("Message from DoSomethingAsync");

		throw new Exception("Some exception");
	}
}
class Program
{
	static void Main(string[] args)
	{
		ISomething something = new Something();
		
		Console.WriteLine("pre something.DoSomethingAsync() without await");
		something.DoSomethingAsync(); // No compiler warning for missing "await" and exception is "swallowed"
		Console.WriteLine("post something.DoSomethingAsync() without await");

		Thread.Sleep(3000);

		// Output:
		// pre something.DoSomethingAsync() without await
		// post something.DoSomethingAsync() without await
		// Message from DoSomethingAsync
	}
}

Solution 4 - C#

An abstract class can be used instead of an interface (in C# 7.3).

// Like interface
abstract class IIO
{
    public virtual async Task<string> DoOperation(string Name)
	{
		throw new NotImplementedException(); // throwing exception
		// return await Task.Run(() => { return ""; }); // or empty do
	}
}

// Implementation
class IOImplementation : IIO
{
	public override async Task<string> DoOperation(string Name)
    {
        return await await Task.Run(() =>
		{
			if(Name == "Spiderman")
				return "ok";
			return "cancel";
		}); 
    }
}

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
QuestionMoriyaView Question on Stackoverflow
Solution 1 - C#svickView Answer on Stackoverflow
Solution 2 - C#DimaView Answer on Stackoverflow
Solution 3 - C#datchungView Answer on Stackoverflow
Solution 4 - C#AlateyView Answer on Stackoverflow