Is it possible to use async/await based methods inside an Acumatica(6.00.1263) PXLongOperation
block?
When I try this, I get this System.InvalidOperationException
:
An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.
My code looks like this:
PXLongOperation.StartOperation(this, async delegate ()
{
FooProcess graph = PXGraph.CreateInstance<FooProcess>();
await graph.ImportDocumentsAsync();
});
I've also tried:
PXLongOperation.StartOperation(this, delegate ()
{
FooProcess graph = PXGraph.CreateInstance<FooProcess>();
graph.ImportDocumentsAsync().GetAwaiter().GetResult();
});
I have added an answer that works, but I hope there is(or will be) a better way to do this, perhaps a different API or overload for long operations that handles async/await natively.