Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading