I am injecting Microsoft.PowerPlatform.Dataverse.Client in Dependency Injection with a tokenProviderFunction
sc.AddSingleton<IOrganizationServiceAsync2, ServiceClient>(provider =>
{
var config ...
var factory ...
var client = new ServiceClient(
new Uri(config....),
factory.GetTokenAsync, true, provider.GetService<ILogger<ServiceClient>>())
{
MaxRetryCount = 5
};
return client;
});
The token factory uses multiple ConfidentialClientApplications, one for each "user" so the ServiceClient has a connection pool
return (await _generator
.AcquireTokenForClient(new[] { $"{resource}/.default" })
.ExecuteAsync(cancellationToken)).AccessToken;
This works in my local vm, in local tests, and when deployed in dev environment. The issue is when integration tests (xunit) run on the Github Runner. The tests run in parallel and when multiple fixtures or test classes are created, the tests hang at the _generator.AcquireTokenForClient(...).ExecuteAsync step.
For those who came here with similar issues, to get this working the github workflow job runner was set to ubuntu-latest-8-cores
We tried with 4-cores but found it timed out intermittently. We will probably split the integration tests so we can use the 4-core runner in parallel jobs