Home > Software design >  Interface task is synchronous, but I need it to return async
Interface task is synchronous, but I need it to return async

Time:10-01

I have downloaded the ShopifySharp project and am trying to make my own executionpolicy by implementing the IRequestExecutionPolicy (https://github.com/nozzlegear/ShopifySharp/blob/master/ShopifySharp/Infrastructure/Policies/RetryExecutionPolicy.cs).

In the example project there is no such "not implemented error". However, a similar implementation puts this "[AsyncStateMachine(typeof(SmartRetryExecutionPolicy.d__5<>))]" before the method.

From what I can tell, making the method async changes the signature and is therefore not implementing the Run method. I'm not very experienced in this area so any help is appreciated.

public delegate Task<RequestResult<T>> ExecuteRequestAsync<T>(CloneableRequestMessage request);

public interface IRequestExecutionPolicy
{
    Task<RequestResult<T>> Run<T>(CloneableRequestMessage requestMessage, ExecuteRequestAsync<T> executeRequestAsync, CancellationToken cancellationToken, int? graphqlQueryCost = null);
}
public class LeakyBucketExecutionPolicy : IRequestExecutionPolicy{
    public LeakyBucketExecutionPolicy(){}

    public async Task<RequestResult<T>> Run<T>(CloneableRequestMessage baseRequest, ExecuteRequestAsync<T> executeRequestAsync, CancellationToken cancellationToken, int? graphqlQueryCost = null)
    {
        //retry code
    }

CodePudding user response:

From what I can tell, making the method async changes the signature

No, it's not. And this is precisely the reason why you will never see async in interfaces.

async keyword is just so you can do awaits and return T instead of return Task<T>. The compiler will wrap it for you. But both the signature, and the way the method is called during the runtime are the same.

  • Related