We started getting the System.InvalidCastException
exception intermittently in our Azure Function app 3 days ago in production and are unable to reproduce the issue in pre-production or locally.
Exception while executing function: GetQueue Error constructing handler for request of type MediatR.IRequestHandler`2[Function Name space,System.Boolean]. Register your handlers with the container. See the samples in GitHub for examples. Failed to connect to Dataverse Unable to cast object of type 'generatedProxy_2' to type 'Microsoft.PowerPlatform.Dataverse.Client.IOrganizationServiceAsync'.
After restarting the Azure function app it starts working again.
it seams like the issue is related to the nuget package Microsoft.PowerPlatform.Dataverse.Client
Looking at the blow links its todo with scaling out.
Unable to cast object of type ‘generatedProxy_2’ to type ‘Microsoft.PowerPlatform.Dataverse.Client.IOrganizationServiceAsync’
Unable to connect to Dataverse: Unable to cast object of type ‘generatedProxy_3’ to type ‘Microsoft.PowerPlatform.Dataverse.Client.IOrganizationServiceAsync’
After upgrading Microsoft.PowerPlatform.Dataverse.Client
from 1.0.9 to 1.1.32 the issue still persists.
Function app runtime version : ~4
.net version 6.0
Our Code on startup looks as follow
services.AddSingleton<IOrganizationServiceAsync, ServiceClient>(provider =>
{
return GetService(d365Settings);
});
public static ServiceClient GetService(D365Settings d365Settings)
{
var client = new ServiceClient(new Uri(d365Settings.OrganizationUri), d365Settings.ClientId, d365Settings.ClientSecret, false);
if (client.IsReady)
return client;
throw new ArgumentException($"Unable to connect to CE, Error: {client.LastError}", client.LastException);
}
csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="6.0.0" />
<PackageReference Include="Microsoft.PowerPlatform.Dataverse.Client" Version="1.1.32" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="....CoreProject1.csproj" />
<ProjectReference Include="....CoreProject2.csproj" />
</ItemGroup>
</Project>
8