Acumatica and IHttpClientFactory

80 Views Asked by At

Is it possible to use IHttpClientFactory with Acumatica? Or can it only be built with mvc? I've built an apiclient for REST calls in a processing screen and am injecting services in a startup.cs page but I get an error:

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 <%

I added a reference to autofac. Is there a configuration I need to add or register services a different way?

1

There are 1 best solutions below

0
On

The error message indicates an attempt to run an asynchronous method within a synchronous method. This is not allowed in C#. You can try to run the asynchronous method in a synchronous task instead. Example:

HttpClient httpClient = [...];
Uri uri = [...]
Task<string> task = Task.Run(() => httpClient.GetStringAsync(uri));
task.Wait();

string httpResult = task.Result;