Quantcast
Channel: Using Optimizely Platforms
Viewing all articles
Browse latest Browse all 8020

What is the best way to call a async method in a sync method?

$
0
0

Hi there,

I did a bit of research on how to call an async method in a sync method in Epi, but I could not find the "final" recommendation. So let's say inside of the method ProcessPayment present in the IPaymentPlugin I want to call an async method, what is the best way? I found the async helper below but I'm not sure if Episer recommends it. Let me know your thoughts, thank you!

public static class AsyncHelper  
{
    private static readonly TaskFactory _taskFactory = new
        TaskFactory(CancellationToken.None,
                    TaskCreationOptions.None,
                    TaskContinuationOptions.None,
                    TaskScheduler.Default);
    public static TResult RunSync<TResult>(Func<Task<TResult>> func)
        => _taskFactory
            .StartNew(func)
            .Unwrap()
            .GetAwaiter()
            .GetResult();
    public static void RunSync(Func<Task> func)
        => _taskFactory
            .StartNew(func)
            .Unwrap()
            .GetAwaiter()
            .GetResult();
}

Episerver version:

CMS:11.20.0

Commerce:13.25.0


Viewing all articles
Browse latest Browse all 8020

Trending Articles