c# - 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;
}
解决方案
问题是因为您试图将 .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 的调用
推荐阅读
- html - Div 在缩小而不是随着页面缩小时被推到页面之外?
- java - 如何在这里修复 java.lang.IndexOutOfBoundsException 错误?
- r - 基于R中的列绘制堆积条
- r - 将数据从行移动到 R 中的列,忽略缺失值并根据转置列添加列
- javascript - 如何修复“用户 \'root\'@\'localhost\' 的访问被拒绝,错误号 1045
- r - 如何将 ShinyR 功能与 UI 集成?
- google-cloud-platform - 永久允许 Google Cloud SDK 访问您在 Colaboratory 的 Google 帐户
- php - 强制免费送货而不隐藏 WooCommerce 中的所有其他送货选项
- python - pytorch 计算成对差异:NumPy 与 PyTorch 和不同 PyTorch 版本的结果不正确
- javascript - 为什么这个 JavaScript 画布绘图应用程序滞后?