Jimmy T.
Jimmy T.

Reputation: 121

Azure Function Using Graph I get "Could not load file or assembly 'System.Text.Json"

In Visual Studio 19, created a .NET Framework 461 Azure Function. Added the Microsoft Graph Nuget package. Make a call to create new GraphServiceClient() and I get

Could not load file or assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

System.Text.Json IS IN THE BIN folder. Anyone experiencing this? Any suggestions?

BTW - the Azure Function is out of the box, created by VS19. I simply added the Nuget package and ran this code (token IS valid - and it happens on the new GraphServiceClient(...) line:

public static async Task<GraphServiceClient> GraphApiClient()
{
    IConfidentialClientApplication confidentialClient = ConfidentialClientApplicationBuilder
        .Create(clientId)
        .WithClientSecret(clientSecret)
        .WithAuthority(new Uri($"https://login.microsoftonline.com/{tenantId}/v2.0"))
        .Build();

    // Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
    var authResult = await confidentialClient
            .AcquireTokenForClient(scopes)
            .ExecuteAsync().ConfigureAwait(false);

    var token = authResult.AccessToken;
    // Build the Microsoft Graph client. As the authentication provider, set an async lambda
    // which uses the MSAL client to obtain an app-only access token to Microsoft Graph,
    // and inserts this access token in the Authorization header of each API request. 
    GraphServiceClient graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
        {
            // Add the access token in the Authorization header of the API request.
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
        })
    );
    return graphServiceClient;
}

Upvotes: 1

Views: 1534

Answers (2)

Stefan Fachmann
Stefan Fachmann

Reputation: 545

Try to add _FunctionsSkipCleanOutput under PropertyGroup tag like this

  <PropertyGroup>
   ..............
   <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
  </PropertyGroup>

in your .csproj azure function project file

p.s.

In my case it was .Net 6

Upvotes: 1

Thiago Custodio
Thiago Custodio

Reputation: 18387

The problem is because you're trying to combine .NET 5 ('core 5' with .net Framework 4.6.1). I strongly recommend you don't start an Azure Function project using .NET Framework for compabitility reasons such as this. Even if you use Newtonsoft.Json, it will conflict with the version .NET function runtime uses.

The best you can do:

1-upgrade and use .net core 3.1 or higher

2-downgrade microsoft graph package library to a version compatible with .net framework 4.6.1 (if it exists)

3-stick with version 4.6.1 (don't recommend), implement the calls to Microsoft Graph using REST APIs directly

Upvotes: 1

Related Questions