microsoft / PowerPlatform-DataverseServiceClient

Code Replica for Microsoft.PowerPlatform.Dataverse.Client and supporting nuget packages.
MIT License
284 stars 52 forks source link

Failure to retrieve CRM Dynamics365 connection instance within an Azure Functions group #482

Open Antherhon opened 2 days ago

Antherhon commented 2 days ago

In the last two days we encountered the following problem within an Azure Functions triggered via HTTP Message:

Image

This error was returned randomly during different working hours and is due to a failure of the functions to instantiate the OrganizationService, i.e., the connection to the Dynamics365 CRM.

To instantiate that connection we use the ServiceClient from the Microsoft.PowerPlatform.Dataverse.Client library version 0.4.21.

We tried running diagnostics, but could not trace the problem.

Can you support us in the analysis to trace the cause of this problem?

Those are the pieces of code that are giving us the problem:

var service = new Context().GetOrganizationServiceClientSecret(log);
public class Context
{
    public IOrganizationService GetOrganizationServiceClientSecret(string clientId, string clientSecret, string organizationUri, ILogger logger)
    {
        try
        {
            var conn = new ServiceClient($@"RequireNewInstance=False;AuthType=ClientSecret;url={organizationUri};ClientId={clientId};ClientSecret={clientSecret}");
            return conn;
        }
        catch (Exception ex)
        {
            logger?.LogError($"Error while connecting to CRM {ex.Message}{Environment.NewLine}{ex.StackTrace}");
            throw ex;
        }
    }

    public IOrganizationService GetOrganizationServiceClientSecret(ILogger logger)
    {
        try
        {
            var clientId = Environment.GetEnvironmentVariable("ClientId");
            var clientSecret = Environment.GetEnvironmentVariable("ClientSecret");
            var organizationUri = Environment.GetEnvironmentVariable("OrganizationUri");
            var conn = GetOrganizationServiceClientSecret(clientId, clientSecret, organizationUri, logger);
            return conn;
        }
        catch (Exception ex)
        {
            logger?.LogError($"Error while connecting to CRM {ex.Message}{Environment.NewLine}{ex.StackTrace}");
            throw ex;
        }
    }
}

and as you can see from the error above, we are entering the catch statement.

Our assumption is that the problem is due to heavy load periods on the system