Skip to content

PlatformNotSupportedException: Dynamic code generation is not supported on this platform when using ServiceClient #535

@rajyraman

Description

@rajyraman

When using Microsoft.PowerPlatform.Dataverse.Client inside a C# single file and then dotnet run on it with .NET 10, ServiceClient throws a PlatformNotSupportedException during ConnectAndInitServiceAsync initialization. Seems this is similar to #501

This was the code I tried to run

// .NET 10 Top-level statements
#:package Azure.Identity@*
#:package Microsoft.PowerPlatform.Dataverse.Client@*

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Azure.Identity;
using Azure.Core;

var resourceUrl = "https://xxxxx.crm6.dynamics.com";

// Initialize ServiceClient
var serviceClient = new ServiceClient(new Uri(resourceUrl), async (string resource) => {
    var credential = new DefaultAzureCredential();
    var tokenContext = new TokenRequestContext(new[] { $"{resourceUrl}/.default" });
    var token = await credential.GetTokenAsync(tokenContext);
    return token.Token;
});

if (!serviceClient.IsReady) {
    Console.WriteLine($"Failed to connect to Dataverse: {serviceClient.LastError}");
    return;
}


// Query records using FetchXML
var fetchXml0 = @"<fetch mapping=""logical"" count=""1""><entity name=""account""><attribute name=""parentaccountid"" /><filter type=""or""><condition attribute=""parentaccountid"" operator= ""eq"" value= ""{449207C6-02A5-ED11-AAD1-00224892B4A1}"" /><filter type=""and"" ><condition attribute=""accountid"" operator= ""eq"" value= ""{449207C6-02A5-ED11-AAD1-00224892B4A1}"" /><condition attribute=""parentaccountid"" operator= ""not-null"" /></filter></filter></entity></fetch>";
var results0 = await serviceClient.RetrieveMultipleAsync(new FetchExpression(fetchXml0));
Console.WriteLine($"Retrieved {results0.Entities.Count} records");

Stack trace:

System.PlatformNotSupportedException: Dynamic code generation is not supported on this platform.
   at System.Reflection.Emit.AssemblyBuilder.ThrowDynamicCodeNotSupported()
   at System.Reflection.Emit.AssemblyBuilder.EnsureDynamicCodeSupported()
   at System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access, IEnumerable`1 assemblyAttributes, Assembly callingAssembly)
   at System.Reflection.DispatchProxyGenerator.ProxyAssembly..ctor(AssemblyLoadContext alc)
   at System.Reflection.DispatchProxyGenerator.<>c.<CreateProxyInstance>b__6_0(AssemblyLoadContext x)
   at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Reflection.DispatchProxyGenerator.CreateProxyInstance(Type baseType, Type interfaceType, String interfaceParameter, String proxyParameter)
   at System.Reflection.DispatchProxy.Create[T,TProxy]()
   at System.ServiceModel.Channels.ServiceChannelProxy.CreateProxy[TChannel](MessageDirection direction, ServiceChannel serviceChannel)
   at System.ServiceModel.Channels.ServiceChannelFactory.CreateProxy[TChannel](MessageDirection direction, ServiceChannel serviceChannel)
   at System.ServiceModel.Channels.ServiceChannelFactory.CreateChannel[TChannel](EndpointAddress address, Uri via)
   at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.ChannelFactory`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannelInternal()
   at System.ServiceModel.ClientBase`1.get_Channel()
   at System.ServiceModel.ClientBase`1.get_InnerChannel()
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.ConnectAndInitServiceAsync(OrganizationDetail orgdata, Boolean IsOnPrem, Uri homeRealmUri)
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.DoDirectLoginAsync(Boolean IsOnPrem)
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.InitServiceAsync()
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.GetCachedService(ConnectionService& ConnectionObject)
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.IntilizeService(ConnectionService& ConnectionObject)
   at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.DoLogin(ConnectionService& ConnectionObject)
   at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.CreateServiceConnection(...)

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