PowerPlatform-DataverseServiceClient icon indicating copy to clipboard operation
PowerPlatform-DataverseServiceClient copied to clipboard

ServiceClient.ctor() throws exception when AOT built

Open incheon-kim opened this issue 3 months ago • 1 comments

It is .NET 9.0 / Windows / NativeAOT.

My code works great when it is not built with native aot. Here is what happened.

var clientId = "{my-client-id}";
var orgUrl = "{my-organization-id}";

new ServiceClient(
                client._Certificate, StoreName.My, client._Certificate?.Thumbprint,
                new Uri($"https://{orgUrl}/"),
                false,
                null,
                clientId,
                new Uri("http://localhost/"),
                Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance);

and it throws

Microsoft.PowerPlatform.Dataverse.Client.Utils.DataverseConnectionException: Failed to connect to Dataverse
 ---> System.ArgumentException: 'Microsoft.Extensions.Logging.LoggerFilterOptions', on 'Microsoft.Extensions.Options.OptionsMonitor`1[TOptions]' violates the constraint of type 'TOptions'.
   at Internal.Reflection.Execution.ConstraintValidator.EnsureSatisfiesClassConstraints(Type[], Type[], Object, ConstraintValidator.SigTypeContext) + 0x12b
   at Internal.Reflection.Execution.ConstraintValidator.EnsureSatisfiesClassConstraints(Type, Type[]) + 0x44
   at Internal.Reflection.Execution.ExecutionEnvironmentImplementation.TryGetConstructedGenericTypeForComponents(RuntimeTypeHandle, RuntimeTypeHandle[], RuntimeTypeHandle&) + 0x116
   at System.Reflection.Runtime.TypeInfos.RuntimeConstructedGenericTypeInfo.GetRuntimeTypeHandleIfAny(RuntimeTypeInfo, RuntimeTypeInfo[], RuntimeConstructedGenericTypeInfo.TryGetConstructedGenericTypeDelegate) + 0x83
   at System.Reflection.Runtime.TypeInfos.RuntimeConstructedGenericTypeInfo.GetRuntimeConstructedGenericTypeInfo(RuntimeTypeInfo, RuntimeTypeInfo[]) + 0x64
   at System.Reflection.Runtime.TypeInfos.RuntimeTypeInfo.MakeGenericType(Type[]) + 0x1fc
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateOpenGeneric(ServiceDescriptor, Type, CallSiteChain, Int32) + 0xac
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateOpenGeneric(Type, CallSiteChain) + 0xad
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type, CallSiteChain) + 0x53
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xa4
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type, Type, CallSiteChain, ParameterInfo[], Boolean) + 0x75
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache, Type, Type, CallSiteChain) + 0x175
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor, Type, CallSiteChain, Int32) + 0x164
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type, CallSiteChain) + 0x8c
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type, CallSiteChain) + 0x3d
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xa4
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type, Type, CallSiteChain, ParameterInfo[], Boolean) + 0x75
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache, Type, Type, CallSiteChain) + 0x33d
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor, Type, CallSiteChain, Int32) + 0x164
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type, CallSiteChain) + 0x8c
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type, CallSiteChain) + 0x3d
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xa4
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType) + 0x5b
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xa4
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type, ServiceProviderEngineScope) + 0x2e
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider, Type) + 0x3e
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider) + 0x29
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite, RuntimeResolverContext, ServiceProviderEngineScope, RuntimeResolverLock) + 0xa7
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite, RuntimeResolverContext) + 0x34
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite, TArgument) + 0x8e
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite, ServiceProviderEngineScope) + 0x29
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider) + 0x2e
   at Microsoft.PowerPlatform.Dataverse.Client.Auth.AuthProcessor.<ExecuteAuthenticateServiceProcessAsync>d__0.MoveNext() + 0x13cb
--- End of stack trace from previous location ---
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.<ConnectAndInitServiceAsync>d__204.MoveNext() + 0xb3
--- End of stack trace from previous location ---
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.<DoDirectLoginAsync>d__185.MoveNext() + 0x81
--- End of stack trace from previous location ---
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.<InitServiceAsync>d__184.MoveNext() + 0x2235
--- End of stack trace from previous location ---
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.GetCachedService(ConnectionService&) + 0xf7
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.IntilizeService(ConnectionService&) + 0x13
   at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.CreateServiceConnection(Object, AuthenticationType, String, String, String, NetworkCredential, String, SecureString, String, String, String, Boolean, Boolean, OrganizationDetail, String, Uri, PromptBehavior, OrganizationWebProxyClientAsync, String, StoreName, X509Certificate2, Uri, Boolean, Boolean, Version, ILogger, String) + 0xbd6
   --- End of inner exception stack trace ---
   at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.CreateServiceConnection(Object, AuthenticationType, String, String, String, NetworkCredential, String, SecureString, String, String, String, Boolean, Boolean, OrganizationDetail, String, Uri, PromptBehavior, OrganizationWebProxyClientAsync, String, StoreName, X509Certificate2, Uri, Boolean, Boolean, Version, ILogger, String) + 0xe55
   at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient..ctor(X509Certificate2, StoreName, String, Uri, Boolean, OrganizationDetail, String, Uri, ILogger) + 0x18f

if i set true for useUniqueInstance it throws below.

new ServiceClient(
                client._Certificate, StoreName.My, client._Certificate?.Thumbprint,
                new Uri($"https://{orgUrl}/"),
                true,
                null,
                clientId,
                new Uri("http://localhost/"),
                Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance);
Microsoft.PowerPlatform.Dataverse.Client.Utils.DataverseConnectionException: Failed to connect to Dataverse
 ---> System.ArgumentException: 'Microsoft.Extensions.Logging.LoggerFilterOptions', on 'Microsoft.Extensions.Options.OptionsMonitor`1[TOptions]' violates the constraint of type 'TOptions'.
   at Internal.Reflection.Execution.ConstraintValidator.EnsureSatisfiesClassConstraints(Type[], Type[], Object, ConstraintValidator.SigTypeContext) + 0x12b
   at Internal.Reflection.Execution.ConstraintValidator.EnsureSatisfiesClassConstraints(Type, Type[]) + 0x44
   at Internal.Reflection.Execution.ExecutionEnvironmentImplementation.TryGetConstructedGenericTypeForComponents(RuntimeTypeHandle, RuntimeTypeHandle[], RuntimeTypeHandle&) + 0x116
   at System.Reflection.Runtime.TypeInfos.RuntimeConstructedGenericTypeInfo.GetRuntimeTypeHandleIfAny(RuntimeTypeInfo, RuntimeTypeInfo[], RuntimeConstructedGenericTypeInfo.TryGetConstructedGenericTypeDelegate) + 0x83
   at System.Reflection.Runtime.TypeInfos.RuntimeConstructedGenericTypeInfo.GetRuntimeConstructedGenericTypeInfo(RuntimeTypeInfo, RuntimeTypeInfo[]) + 0x64
   at System.Reflection.Runtime.TypeInfos.RuntimeTypeInfo.MakeGenericType(Type[]) + 0x1fc
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateOpenGeneric(ServiceDescriptor, Type, CallSiteChain, Int32) + 0xac
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateOpenGeneric(Type, CallSiteChain) + 0xad
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type, CallSiteChain) + 0x53
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xa4
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type, Type, CallSiteChain, ParameterInfo[], Boolean) + 0x75
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache, Type, Type, CallSiteChain) + 0x175
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor, Type, CallSiteChain, Int32) + 0x164
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type, CallSiteChain) + 0x8c
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type, CallSiteChain) + 0x3d
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xa4
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type, Type, CallSiteChain, ParameterInfo[], Boolean) + 0x75
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache, Type, Type, CallSiteChain) + 0x33d
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor, Type, CallSiteChain, Int32) + 0x164
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type, CallSiteChain) + 0x8c
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type, CallSiteChain) + 0x3d
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xa4
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType) + 0x5b
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xa4
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type, ServiceProviderEngineScope) + 0x2e
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider, Type) + 0x3e
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider) + 0x29
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite, RuntimeResolverContext, ServiceProviderEngineScope, RuntimeResolverLock) + 0xa7
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite, RuntimeResolverContext) + 0x34
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite, TArgument) + 0x8e
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite, ServiceProviderEngineScope) + 0x29
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider) + 0x2e
   at Microsoft.PowerPlatform.Dataverse.Client.Auth.AuthProcessor.<ExecuteAuthenticateServiceProcessAsync>d__0.MoveNext() + 0x13cb
--- End of stack trace from previous location ---
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.<ConnectAndInitServiceAsync>d__204.MoveNext() + 0xb3
--- End of stack trace from previous location ---
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.<DoDirectLoginAsync>d__185.MoveNext() + 0x81
--- End of stack trace from previous location ---
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.<InitServiceAsync>d__184.MoveNext() + 0x2235
--- End of stack trace from previous location ---
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.GetCachedService(ConnectionService&) + 0xf7
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.IntilizeService(ConnectionService&) + 0x13
   at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.CreateServiceConnection(Object, AuthenticationType, String, String, String, NetworkCredential, String, SecureString, String, String, String, Boolean, Boolean, OrganizationDetail, String, Uri, PromptBehavior, OrganizationWebProxyClientAsync, String, StoreName, X509Certificate2, Uri, Boolean, Boolean, Version, ILogger, String) + 0xbd6
   --- End of inner exception stack trace ---
   at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.CreateServiceConnection(Object, AuthenticationType, String, String, String, NetworkCredential, String, SecureString, String, String, String, Boolean, Boolean, OrganizationDetail, String, Uri, PromptBehavior, OrganizationWebProxyClientAsync, String, StoreName, X509Certificate2, Uri, Boolean, Boolean, Version, ILogger, String) + 0xe55
   at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient..ctor(X509Certificate2, StoreName, String, Uri, Boolean, OrganizationDetail, String, Uri, ILogger) + 0x18f
   at Fasoo.Wrapsody.Microsoft365.Bridge.BridgeClient.PowerAutomate.Initialize(BridgeClient, String, String) + 0xd0
   at Fasoo.Wrapsody.Microsoft365.Bridge.BridgeClient.InitializeWithStore(String, String, String, String, String) + 0xef

I don't know why it throws logger-related exception while i set logger as NullLogger.

incheon-kim avatar Sep 29 '25 05:09 incheon-kim

I updated Microsoft.Exntension.DependencyInjection from 3.1.8 to 9.0.9 and now i got this (shorter) stack trace.

System.ArgumentException: Generic implementation type 'Microsoft.Extensions.Options.OptionsMonitor`1' has a DefaultConstructorConstraint ('new()' constraint), but the generic service type 'Microsoft.Extensions.Options.IOptionsMonitor`1' doesn't.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.ValidateTrimmingAnnotations(Type, Type[], Type, Type[]) + 0x188
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.Populate() + 0x137
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1, ServiceProviderOptions) + 0x13c
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection, ServiceProviderOptions) + 0x34
   at Microsoft.PowerPlatform.Dataverse.Client.Utils.ClientServiceProviders.BindServiceProviders() + 0x16c
   at Microsoft.PowerPlatform.Dataverse.Client.Utils.ClientServiceProviders.get_Instance() + 0x18
   at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient..ctor(X509Certificate2, StoreName, String, Uri, Boolean, OrganizationDetail, String, Uri, ILogger) + 0x39

incheon-kim avatar Sep 29 '25 05:09 incheon-kim