How to add parameters to an async function action parameter in c#

Advertisements

I have the method below

protected async Task ProcessExecuteResponseAsync(string errorMessage, 
            Func<Task> failureAction = null)
{
    if (isOk == false)
    {
        if (failureAction != null)
        {
            var failureResult = failureAction.Invoke();
            await failureResult;
        }
    }
}

This works perfectly

However, now I want to pass error message to the failureAction action

What is the syntax for that?

I tried

protected async Task ProcessExecuteResponseAsync(string errorMessage, 
            Func<Task, string> failureAction = null)
{
    if (isOk == false)
    {
        if (failureAction != null)
        {
            var failureResult = failureAction.Invoke(errorMessage);
            await failureResult;
        }
    }
}

But I get error that string cannot be converted to Task

>Solution :

You have to change the order of the generic arguments from Func<Task, string> to Func<string, Task>.

The last argument is always the return value and the ones before are the input arguments.

Leave a ReplyCancel reply