Home > Net >  Add async in a non-async interfaces
Add async in a non-async interfaces

Time:04-06

I have a legacy .NET Framework application that’s been running for a decade.

There is an interface:

interface IService
{
   void Run();
}

And this interface is implemented in many existing classes. There is a central manager that picks up all classes implementing IService and calls the Run() method on each class. (This code is already available and runs as expected).

Now, I want to add one more class that implements this same IService, but I want Run() to be an async Task instead of a void, as I am dealing with API calls in this new class. I don't want to disturb the existing functionality of how the central manager works.

CodePudding user response:

This cannot be done in a way that maintains backward compatibility—and that’s a good thing.

Remember, the entire purpose of an interface is to guarantee that all callers can interact with a concrete implementation of that interface without knowing anything about the concrete type itself. Implementing a method as async changes that method’s interface—i.e., by returning a Task<> and potentially expecting an await keyword, depending on how it’s called—and, therefore, breaks the interface. You can change the signature, but by definition that breaks backward compatibility with that interface.

Given this, the textbook solution would be to either to change the interface to use async—thus requiring all implementations to be updated—or create a new async version of your interface (e.g., IServiceAsync) which is used by implementations and callers expecting asynchronous functionality, as suggested in @Ryan Wilson’s comment. The latter likely makes more sense given that you are working with legacy code; that’s especially true if you don’t own all of the implementations.

Obviously, if appropriate, your two interfaces can derive from a common interface which shares any non-async methods, thus allowing them to be used interchangeably in those scenarios. This is useful if you have clients that don’t rely on any of the implicated async methods.

Your central manager can be updated to look for both IService and IServiceAsync, and conditionally call e.g., RunAsync() on the latter. Just make sure you’re truly taking advantage of the asynchronous capabilities in that case (e.g., by adding these to a task queue and processing tasks as they complete).

I recognize that your objective was to avoid updating your central manager. Unfortunately, though, there’s no way to accomplish this while also taking advantage of any asynchronous processing desired for the API call.

CodePudding user response:

Just wrap your new async functionality in a non-async way.

class AsyncService : IService
{
    public void Run()
    {
        myasyncFunction().GetAwaiter().GetResult();
    }
} 
  • Related