首页 > 解决方案 > Azure Function Using Graph 我得到“无法加载文件或程序集'System.Text.Json”

问题描述

在 Visual Studio 19 中,创建了一个 .NET Framework 461 Azure Function。添加了 Microsoft Graph Nuget 包。拨打电话创建新的GraphServiceClient(),我得到

无法加载文件或程序集“System.Text.Json,Version=5.0.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51”或其依赖项之一。该系统找不到指定的文件。

System.Text.Json 在 BIN 文件夹中。有人遇到这种情况吗?有什么建议么?

顺便说一句 - Azure 功能开箱即用,由 VS19 创建。我只是添加了 Nuget 包并运行了这段代码(令牌是有效的 - 它发生new GraphServiceClient(...)在线上:

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;
}

标签: c#.netazurefunctiongraph

解决方案


问题是因为您试图将 .NET 5('core 5' 与 .net Framework 4.6.1)结合起来。出于兼容性原因,我强烈建议您不要使用 .NET Framework 启动 Azure Function 项目。即使您使用 Newtonsoft.Json,它也会与 .NET 函数运行时使用的版本冲突。

你能做到的最好的:

1-升级并使用 .net core 3.1 或更高版本

2-将 microsoft graph 包库降级为与 .net framework 4.6.1 兼容的版本(如果存在)

3-坚持使用 4.6.1 版本(不推荐),直接使用 REST API 实现对 Microsoft Graph 的调用


推荐阅读