question

PanagiotopoulosDiane-1729 avatar image
3 Votes"
PanagiotopoulosDiane-1729 asked saldana-msft edited

Error loading assembly using Graph API

I am trying to write a function app that uses the Graph SDK. I have declared dependencies on:

  • Microsoft.Azure.Functions.Extensions 1.1.0

  • Microsoft.Graph - 4.1

  • Microsoft.Graph.Auth - 1.0.0-preview

  • Microsoft.Net.SDK.Extensions - 3.0.13

When I try to run a test request, I get the following error:

 An exception of type 'System.IO.FileNotFoundException' occurred in System.Private.CoreLib.dll but was not handled in user code: 'Could not load file or assembly 'System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.'
    at System.Text.Json.JsonSerializerOptions.GetWriterOptions()
    at System.Text.Json.JsonSerializer.Serialize[TValue](TValue& value, Type inputType, JsonSerializerOptions options)
    at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
    at Microsoft.Graph.Serializer.SerializeObject(Object serializeableObject)
    at Microsoft.Graph.BaseRequest.<SendRequestAsync>d__40.MoveNext()
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
    at Microsoft.Graph.BaseRequest.<SendAsync>d__34`1.MoveNext()
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
    at {<!-- -->{my code}}


There are clearly two different versions of System.Text.Encodings.Web being declared by the packages I am using.

If I try to update my .csproj file to explicitly reference 5.0.0:

 <PackageReference Include="System.Text.Encodings.Web" Version="5.0.0" />

I get the following compile time error:

 [...]\source\API-AzureExternalIdentity\API-AzureExternalIdentity.csproj : error NU1605: Detected package downgrade: System.Text.Encodings.Web from 5.0.1 to 5.0.0. Reference the package directly from the project to select a different version. 
 [...]\source\API-AzureExternalIdentity\API-AzureExternalIdentity.csproj : error NU1605:  API-AzureExternalIdentity -> Microsoft.Graph 4.1.0 -> Microsoft.Graph.Core 2.0.1 -> System.Text.Json 5.0.2 -> System.Text.Encodings.Web (>= 5.0.1)
 [...]\source\API-AzureExternalIdentity\API-AzureExternalIdentity.csproj : error NU1605:  API-AzureExternalIdentity -> System.Text.Encodings.Web (>= 5.0.0)

I tried using the Modules panel in Visual Studio to figure out which version of System.Text.Encodings.Web is actually being loaded (as described in https://michaelscodingspot.com/how-to-resolve-net-reference-and-nuget-package-version-conflicts/), and, uh... what it showed me was that I'm using version 4.700.21, which doesn't exist anywhere that I've been able to look.

What combination of packages do I actually need to use in order to use the Graph SDK in a Function App that is using the DotNet Core 3.1 runtime?

dotnet-csharpazure-functionsmicrosoft-graph-sdk
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @PanagiotopoulosDiane-1729
I was able to repro this locally and I have reached out to team for further input. It seems to be related to conflict created by dependency for func.exe and the function project itself for the assembly "System.Text.Encodings.Web"


Regarding .NET assembly versions, here are some information to help clarify confusion:
Each assembly has 3 ways of expressing version information. Two of them are explained here:
Assembly versioning
The last one is the File Version, which is also available for other executables/dlls on Windows. This is displayed on the Version tab of the Windows file properties.
The version that you see in Modules view of Visual Studio is the "File Version". .NET Clr does not use it for resolving the assembly name/Version.
To get the assembly version being used by CLR, you can use the following PowerShell script:

[System.Reflection.AssemblyName]::GetAssemblyName("<FullPath of Assembly>\System.Text.Encodings.Web.dll").FullName

To investigate any assembly probe/load related issues, environment variables are used as mentioned here.

0 Votes 0 ·
AnuragSingh-MSFT avatar image
7 Votes"
AnuragSingh-MSFT answered sathisk commented

Hi @PanagiotopoulosDiane-1729

I was able to get around the issue by adding the following in .csproj file under <PropertyGroup>

 <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>

A new feature was added few months back that takes a pre-determined list of assemblies and deletes them from your output directory after a build or publish. These assemblies are already installed on the server, therefore deleting them results in smaller published package.
But in some cases it doesn't work -- for example if you're trying to use a newer version than is installed on the server. Specifying _FunctionsSkipCleanOutput skips that deleting step and leaves all the assemblies there, resulting in a bigger package size, but ensuring that all the assemblies you need are deployed with your app. This effectively reverts you back to the same behavior we had for several years. More information is available here.

Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Very interesting explanation, thank you! I think it is a better solution for me to upgrade to the 5.0 runtime (where my app and Azure Functions run in separate processes from each other) but I appreciate knowing that the workaround exists if I need it.

1 Vote 1 ·

This fixed the problem for me

1 Vote 1 ·

This fixed the problem for me too.
I was trying to perform D365 CRUD operation in .net core 3 azure function but facing the error 'Could not load file or assembly 'System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.' [Referenced the blog https://powerobjects.com/dynamics-365/authentication-to-dynamics-365-using-azure-function-v3-net-core/]
This System.Text.Encodings.Web error was a blocker. Merely adding <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput> under </PropertyGroup> in .csproj solved the issue.



1 Vote 1 ·
sathisk avatar image sathisk jyotiyadav-6254 ·

Indeed, it is fixed for Microsoft.PowerPlatform.Dataverse.Client dll run in Azure function v3

0 Votes 0 ·
PanagiotopoulosDiane-1729 avatar image
0 Votes"
PanagiotopoulosDiane-1729 answered

I still have no idea what the solution is when using dotnet 3.1, but I found this demo application using dotnet 5.0 and it works smoothly: https://github.com/microsoftgraph/msgraph-training-azurefunction-csharp/tree/main/demo/GraphTutorial

My development is unblocked but I am still hoping to find an answer for the dotnet 3.1 experience.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.