Skip to content

Failed to Clone connection #467

Open
@MitchellW-DWL

Description

@MitchellW-DWL

Hi guys - been seeing a pretty transient error occasionally on any request to Dynamics in asp.net core WebApi

Version: 1.0.39
(Upgraded to 1.1.32 at the time of writing this but dont expect this will have resolved it - will update if i see this come up again)

Code Context

So using .net8 (Only recently moved to net8 from net6 within last couple of weeks but was seeing it in net6 too)

My DI is setup like so:

sc.AddSingleton<IServiceClientFactory, ServiceClientFactory>();
sc.AddScoped<ServiceClient>(sp => sp.GetRequiredService<IServiceClientFactory>().CreateServiceClient());

Then the ServiceClientFactory singleton is just:

public class ServiceClientFactory : IServiceClientFactory
{
    private readonly Lazy<ServiceClient> _serviceClient;

    public ServiceClientFactory(DynamicsConfiguration dynamicsConfiguration, ILogger<ServiceClient> logger)
    {
        _serviceClient = new Lazy<ServiceClient>(() => new ServiceClient(
            new Uri(dynamicsConfiguration.CrmUrl),
            dynamicsConfiguration.ClientId,
            dynamicsConfiguration.AppKey,
            false,
            logger));
    }

    public ServiceClient CreateServiceClient()
        => _serviceClient.Value.Clone();
}

Would appreciate any recommendations on

Here's a trace for the error what I'm seeing:

StackTrace

Microsoft.PowerPlatform.Dataverse.Client.Utils.DataverseConnectionException: Failed to Clone Connection
    ---> Microsoft.PowerPlatform.Dataverse.Client.Utils.DataverseOperationException: Failed constructing cloned connection. debugstate=1
    ---> System.AggregateException: One or more errors occurred. (Exception - Failed to lookup current user)
    ---> Microsoft.PowerPlatform.Dataverse.Client.Utils.DataverseOperationException: Exception - Failed to lookup current user
    ---> System.NullReferenceException: Object reference not set to an instance of an object.
      at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.Command_WebExecuteAsync(String queryString, String body, HttpMethod method, Dictionary`2 customHeaders, String contentType, String errorStringCheck, Guid callerId, Boolean disableConnectionLocking, Int32 maxRetryCount, TimeSpan retryPauseTime, Uri uriOfInstance, Guid requestTrackingId, CancellationToken cancellationToken)
      at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.Command_WebAPIProcess_ExecuteAsync(OrganizationRequest req, String logMessageTag, Boolean bypassPluginExecution, MetadataUtility metadataUtlity, Guid callerId, Boolean disableConnectionLocking, Int32 maxRetryCount, TimeSpan retryPauseTime, CancellationToken cancellationToken, Uri uriOfInstance, Boolean inLoginFlow)
      at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.GetWhoAmIDetails(IOrganizationService dvService, Guid trackingID)
      --- End of inner exception stack trace ---
      at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.GetWhoAmIDetails(IOrganizationService dvService, Guid trackingID)
      at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.<get_SystemUser>b__62_0()
      --- End of inner exception stack trace ---
      at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
      at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
      at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.get_SystemUser()
      at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.SetClonedProperties(ServiceClient sourceClient)
      --- End of inner exception stack trace ---
      at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.SetClonedProperties(ServiceClient sourceClient)
      at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.Clone(Assembly strongTypeAsm, ILogger logger)
      --- End of inner exception stack trace ---
      at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.Clone(Assembly strongTypeAsm, ILogger logger)
      at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.Clone(ILogger logger)
      at DWL.Lib.Dynamics.Services.ServiceClientFactory.CreateServiceClient()
      at DWL.Lib.Dynamics.Extensions.ServiceCollectionExtensions.<>c.<AddDwlDynamicsServices>b__0_0(IServiceProvider sp)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
      at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
      at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
      at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
      at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
      at MediatR.Wrappers.RequestHandlerWrapperImpl`2.<>c__DisplayClass1_0.<Handle>g__Handler|0()

Looking through some other issues I've noticed #362 seems to be throwing the same error albeit in a different context, but is also making use of .Clone()

Metadata

Metadata

Assignees

No one assigned

    Labels

    InvestigatingWe are looking into this issue at this time.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions