Repository navigation
Description
I‘m getting this issue occasionally for azure function app, I'm using the latest verion of Microsoft.PowerPlatform.Dataverse.Client(0.6.1).
Full Exception :
Microsoft.PowerPlatform.Dataverse.Client.Utils.DataverseConnectionException : Failed to connect to Dataverse ---> System.AggregateException : One or more errors occurred. (Unable to cast object of type 'generatedProxy_2' to type 'Microsoft.PowerPlatform.Dataverse.Client.IOrganizationServiceAsync'.) ---> Unable to cast object of type 'generatedProxy_2' to type 'Microsoft.PowerPlatform.Dataverse.Client.IOrganizationServiceAsync'.
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task`1.get_Result()
at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.GetCachedService(ConnectionService& ConnectionObject)
at Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.IntilizeService(ConnectionService& ConnectionObject)
at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.CreateServiceConnection(Object externalOrgServiceProxy,AuthenticationType requestedAuthType,String hostName,String port,String orgName,NetworkCredential credential,String userId,SecureString password,String domain,String Geo,String claimsHomeRealm,Boolean useSsl,Boolean useUniqueInstance,OrganizationDetail orgDetail,String clientId,Uri redirectUri,PromptBehavior promptBehavior,OrganizationWebProxyClientAsync externalOrgWebProxyClient,String certificateThumbPrint,StoreName certificateStoreName,X509Certificate2 certificate,Uri instanceUrl,Boolean isCloned,Boolean useDefaultCreds,Version incomingOrgVersion,ILogger externalLogger,String tokenCacheStorePath)
---> (Inner Exception #0) System.InvalidCastException : Unable to cast object of type 'generatedProxy_2' to type 'Microsoft.PowerPlatform.Dataverse.Client.IOrganizationServiceAsync'.
at async Microsoft.PowerPlatform.Dataverse.Client.ConnectionService.InitServiceAsync()<---
End of inner exception
at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.CreateServiceConnection(Object externalOrgServiceProxy,AuthenticationType requestedAuthType,String hostName,String port,String orgName,NetworkCredential credential,String userId,SecureString password,String domain,String Geo,String claimsHomeRealm,Boolean useSsl,Boolean useUniqueInstance,OrganizationDetail orgDetail,String clientId,Uri redirectUri,PromptBehavior promptBehavior,OrganizationWebProxyClientAsync externalOrgWebProxyClient,String certificateThumbPrint,StoreName certificateStoreName,X509Certificate2 certificate,Uri instanceUrl,Boolean isCloned,Boolean useDefaultCreds,Version incomingOrgVersion,ILogger externalLogger,String tokenCacheStorePath)
at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient.ConnectToService(String connectionString,ILogger logger)
at Microsoft.PowerPlatform.Dataverse.Client.ServiceClient..ctor(String dataverseConnectionString,ILogger logger)
at myFunction.Startup.<>c.b__2_0(IServiceProvider s) at D:\XXX\AzureFunction\AzureFunctions\myFunction\Startup.cs : 16
at DryIoc.Microsoft.DependencyInjection.DryIocAdapter.<>c__DisplayClass3_0.b__0(IResolverContext r) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\DryIocAdapter.cs : 156
at DryIoc.Registrator.<>c__DisplayClass27_0.b__0(IResolverContext r) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs : 4550
at System.Linq.Expressions.Interpreter.FuncCallInstruction`3.Run(InterpretedFrame frame)
at System.Linq.Expressions.Interpreter.Interpreter.Run(InterpretedFrame frame)
at System.Linq.Expressions.Interpreter.LightLambda.Run(Object[] arguments)
at Thunk(Func`2 ,IResolverContext )
at DryIoc.Factory.<>c__DisplayClass26_0.b__2() at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs : 6605
at DryIoc.Scope.TryGetOrAdd(ImMap`1 items,Int32 id,CreateScopedValue createValue,Int32 disposalOrder) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs : 7849
at DryIoc.Scope.GetOrAdd(Int32 id,CreateScopedValue createValue,Int32 disposalOrder) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs : 7834
at DryIoc.Factory.ApplyReuse(Expression serviceExpr,Request request) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs : 6604
at DryIoc.Factory.GetExpressionOrDefault(Request request) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs : 6564
at DryIoc.Factory.GetDelegateOrDefault(Request request) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs : 6634
at DryIoc.DelegateFactory.GetDelegateOrDefault(Request request) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs : 7740
at DryIoc.Container.ResolveAndCacheDefaultFactoryDelegate(Type serviceType,IfUnresolved ifUnresolved) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs : 211
at DryIoc.Container.DryIoc.IResolver.Resolve(Type serviceType,IfUnresolved ifUnresolved) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs : 196
at Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection.ScopedServiceProvider.GetService(Type serviceType) at D:\a_work\1\s\src\WebJobs.Script.WebHost\DependencyInjection\ScopedServiceProvider.cs : 25
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,Type type,Type requiredBy,Boolean isDefaultParameterRequired)
at lambda_method(Closure ,IServiceProvider ,Object[] )
at Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance[T](IServiceProvider serviceProvider) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DefaultJobActivator.cs : 37
at Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance[T](IFunctionInstanceEx functionInstance) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DefaultJobActivator.cs : 32
at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory`1.<>c__DisplayClass1_1.<.ctor>b__0(IFunctionInstanceEx i) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\ActivatorInstanceFactory.cs : 20
at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory`1.Create(IFunctionInstanceEx functionInstance) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\ActivatorInstanceFactory.cs : 26
at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.CreateInstance(IFunctionInstanceEx functionInstance) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs : 44
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ParameterHelper.Initialize() at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs : 799
at async Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsync(IFunctionInstance functionInstance,CancellationToken cancellationToken) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs : 104
Activity
MoranShusheng commentedon Apr 13, 2022
We have encountered the same problem.
There are a lot of errors.
Do you have any progress on this problem?
MattB-msft commentedon Apr 14, 2022
We have not been able to reliably duplicate the problem at this point and are speculating on what is going on.
it would be extremely helpful if you could provide us a repro of this issue or more information about what is going on when it happens?
thanks
MattB
MortenRoemer commentedon May 3, 2022
We have the same issue here. Sadly the project I'm working on is proprietary but I can reproduce the error in a reliable way:
Framework: .NET 6
Function Runtime Version: latest
Steps:
This produces the mentioned error for us every time we start up the function app.
Any pointers what the underlying issues could be? With the right pointers I could try to fix the issue myself and create a pull-request
Edit: We are working with the european tenants of Dynamics so our instances have the pattern
<instancename>.crm4.dynamics.comMattB-msft commentedon May 4, 2022
The client does initialize? Or does it fail and you have a non working client?
MortenRoemer commentedon May 4, 2022
The Client fails to initialize. After that every call to Public members results in NullReferenceExceptions
MattB-msft commentedon May 4, 2022
Can you paste your setup block here?
I assume you have a startup.cs with a public override void Configure(IFunctionsHostBuilder builder) your adding the setup call too?
MattB-msft commentedon May 5, 2022
As an example,... this is what one of our function test cases' startup.cs's looks like:
This is Functions 4.0. ( this is not a formal example, but it is from an internal function we are using )
MortenRoemer commentedon May 5, 2022
Our startup function is a little bit simpler:
MortenRoemer commentedon May 5, 2022
EDIT: This workaround reduced the likelyhood of this issue but not prevented it. See post from 2022-05-09 for more info
But we found a workaround: When we handle the client/secret authentication ourselves it works without issues:
Here is our workaround startup function:
And our helper class that handles the client/secret authentication:
MattB-msft commentedon May 5, 2022
Are you using the 0.6.6 client? If not can you retest on the please .
MortenRoemer commentedon May 5, 2022
I've tested it initially on the 0.6.6 release. There I encountered the error
MattB-msft commentedon May 5, 2022
Thanks, can you tell us about you hosting setup,
What version of azure functions? What version of the azure functions library (nuget) and dependency injection your using?
What OS host?
Dedicated or shared sub for azure?
Thanks
MortenRoemer commentedon May 5, 2022
Here:
Hope that helps
MattB-msft commentedon May 5, 2022
We are, sofar, unable to duplicate this issue.
Can you add an ILogger to your function and capture logs for us of the failure?
To do that... Add the Ilogger ( Created As I showed above, the constructor your using will accept an ILogger as the final parameter ) and include in your host.json
if you are not comfortable sharing the output here on Git,
please go here: Nuget: Microsoft.PowerPlatform.Dataverse.Client
and hit contact owner - middle of the page on the right and send an email referring to this issue
I will respond directly to you via email and you can reply with the log
Thanks for your help in helping us track this down.
TomWinton commentedon May 5, 2022
This may or may not help but wanted to confirm that I was also getting this issue, was using also using version 6.6, after seeing this and not wanting to try MortenRoemers clever workaround I tried going back down to 5.17 with same result.
Azure Function Runtime: latest
Microsoft.NET.Sdk.Functions: 4.1.0
Host OS: Windows
Consumption Plan
.Net 6
Initially I was generating a new ServiceClient each time a request was required with a connection string.
Restarting the function would temporarily resolve the issue, the issue would then occur again after a period of time and then continue to happen, given it occurred in line with a spike in requests to the function I made an assumption (which i think is correct) that it occurs when the function auto scales out.
I implemented an easier but way stupiderer workaround which was that I have a declared a static ServiceClient and during startup I create the assign it.
Now, when I stare at the live logs on Azure I can see that this issue still occurs, but because it occurs during startup Microsoft helpfully abort this scale out attempt and makes a new instance (or maybe just abandons the scale out attempt entirely and restarts the service) which ends up working so when my functions needs it it doesn't bork out.
Either way, seeing as now I make the whole thing crap out when it tries to scale out the issue has to be something to do with reusing invalid info from another instance.
8 remaining items
twiga2013 commentedon Aug 5, 2022
Hi MattB (@MattB-msft)
The changes you recommend made a big difference. The Azure Functions were scaling out and was not see any errors. I will run some additional test under heavy load. Thank You so much for all your help.
mathiasbl commentedon Sep 21, 2022
MattB (@MattB-msft) there's still locking going on when the SOAP endpoint is used. In that case you should use Clone()?
MattB-msft commentedon Sep 21, 2022
Mathias (@mathiasbl) , when using the Async messages you should not be seeing locking on any endpoint, could you clarify what your seeing?
mathiasbl commentedon Sep 21, 2022
I thought so by looking at the source code. An unsupported request, that uses soap, goes down to this method:
PowerPlatform-DataverseServiceClient/src/GeneralTools/DataverseClient/Client/ServiceClient.cs
Line 1677 in dc278e3
MattB-msft commentedon Sep 21, 2022
Correct, this should not lock the channel, are you seeing that happen?
mathiasbl commentedon Nov 17, 2022
No, I misinterpreted the code.
I'm also seeing this issue in my functionapp (v4). Any progress on it?
Fuction was running fine until this morning. This error happens on startup I think.
Once started the function app will keep working.
My setup in DI:
Trace:
mathiasbl commentedon Feb 6, 2023
MattB (@MattB-msft) any progress? Still occasionally happens.
MattB-msft commentedon Feb 14, 2023
We think this may be related to the memory leak issue here #305
Our next update should resolve 305, please let us know after that release if this problem goes away for you
AnastasiiL commentedon Mar 7, 2023
Hello MattB (@MattB-msft). Could you please clarify when we can expect the new release? Thank you!
MattB-msft commentedon Mar 7, 2023
We are doing a good deal of testing on this drop and running it internally for a bit to verify a number of the changes.
So "soon(tm)"
realistically, week of 3/13 is the goal.
MattB-msft commentedon Mar 23, 2023
We believe we have resolved this with the drop of 1.0.39.
I am going to close this thread now. If you experience this again after up taking 1.0.39+, Please reopen with details.
thanks
xairrick commentedon May 9, 2024
No need to re-open, but recently had this error in an Azure Function (TimerTrigger). The timer was set to one minute. At times the previous instance was still running when then next interval triggered. According to the docs, the Singleton lock should have prevent this, but it didn't and there were multiple instances running at the same time. After some period of time of having multiple instances running this error would start to appear.
Obviously, the solution was to prevent multiple instances (which we did with a Durable Function)
caiyiJu commentedon Oct 21, 2024
Hi MattB, we are still encountering this issue when using version 1.1.32. The exception is as below. The issue is happening when there are instances movement. Can you help to check?
Microsoft.PowerPlatform.Dataverse.Client.Utils.DataverseConnectionException : Failed to connect to Dataverse ---> System.AggregateException : One or more errors occurred. (Unable to cast object of type 'generatedProxy_2' to type 'Microsoft.PowerPlatform.Dataverse.Client.IOrganizationServiceAsync'.) ---> Unable to cast object of type 'generatedProxy_2' to type 'Microsoft.PowerPlatform.Dataverse.Client.IOrganizationServiceAsync'.
Environment:
Platform: Windows.
Service: Azure function app.
Service plan: consumption.
kshyju commentedon Nov 26, 2024
Tony (@caiyiJu) Are you still experiencing this issue? Can you provider more details about what use case is causing this error? Is it happening when your app is handling sudden increase in http traffic?