Skip to content

ServiceClient excessively acquires tokens when specifying AccessTokenProviderFunctionAsync and ExternalTokenManagement type #377

@jimmy3912msncom

Description

@jimmy3912msncom

We noticed in our logs after switching over to use the ServiceClient that for some reason it seems to excessively call to acquire an access token. It seems to call the AccessTokenProviderFunctionAsync once when instantiating the serviceClient, to acquire a token, then when you try to execute a request it seems to acquire access tokens again but this times 6 times for each request.

This means if we use the client to make:

  1. 1 API call it will try to make 7 token request calls
  2. 2 API calls it will try to make 13 token request calls
  3. ...

Client details: Microsoft.PowerPlatform.Dataverse.Client Version=[1.0.39]
Target framework: .NET 6.0

We log each of our token requests and although it uses a cache it does add confusion to our service. Below is a sample of code that can be put together to reproduce this.

Also this is a gif of what I see:
dvserviceclientexcesstokens

...
        private int _authTokenAcquiryCount = 0;
        public async Task<string> GetTokenForInstanceAsync(Uri uri)
        {
            _authTokenAcquiryCount++;
            Console.WriteLine("coming in to acquire auth again " + uri);
            return "your token logic";
        }

        protected async Task AnalyzeAsync(bool withRecordingIngestion)
        {
            var targetUri = new Uri("orgUrl");
            var serviceClient = new PowerPlatform.Dataverse.Client.ServiceClient(
                new PowerPlatform.Dataverse.Client.Model.ConnectionOptions
                {
                    AccessTokenProviderFunctionAsync = _ => GetTokenForInstanceAsync(targetUri),
                    AuthenticationType = PowerPlatform.Dataverse.Client.AuthenticationType.ExternalTokenManagement,
                    ServiceUri = targetUri,
                    ClientId = "your client id",
                },
                false,
                new PowerPlatform.Dataverse.Client.Model.ConfigurationOptions()
                {
                    UseWebApi = true
                });

            await serviceClient.ExecuteAsync(new RetrieveMultipleRequest() { Query = new QueryExpression("usersettings") });

            await serviceClient.ExecuteAsync(new RetrieveMultipleRequest() { Query = new QueryExpression("usersettings") });

            await serviceClient.ExecuteAsync(new RetrieveMultipleRequest() { Query = new QueryExpression("usersettings") });
        }
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions