Error loading assembly using Graph API

Panagiotopoulos, Diane 41 Reputation points
2021-08-12T00:50:54.967+00:00

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?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,382 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,837 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,385 questions
{count} votes

Accepted answer
  1. AnuragSingh-MSFT 20,671 Reputation points
    2021-08-16T16:30:07.937+00:00

    Hi @Panagiotopoulos, Diane

    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.

    7 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Panagiotopoulos, Diane 41 Reputation points
    2021-08-12T19:52:00.18+00:00

    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.

    0 comments No comments