c# - Azure Functions Newtonsoft.Json 加载错误

标签 c# azure json.net azure-functions

这让我发疯。我在使用 Newtonsoft.Json 时遇到 Azure 函数加载错误。

这是一个 .netstandard2.0 项目,我安装了版本 11.0.2。我在网上查看过,大多数其他人都使用旧版本的 Microsoft.NET.Sdk.Functions : 1.0.13 但我使用的是 1.0.14

请参阅下面的我的项目文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
    <RootNamespace>modoapi</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Autofac" Version="4.2.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta5" />
    <PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="3.0.0-beta5" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.14" />
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

我已经查看了我的项目的所有依赖项,它们都指向 Newtonsoft.Json 11.0.2。

该文件存在于我的输出 bin 文件夹中。我已经清理了溶液。更新到最新的 VS 和 Azure Functions 和 WebJobs 工具 15.0.40608.0

函数应用程序启动正常,但是当我向队列添加消息以调用触发器时,我在控制台中收到以下错误。

[12/07/2018 10:56:36] Executed 'ExecuteWorkItem' (Failed, Id=6d87f5e9-c331-4934-a3f3-b9bebf756b54) [12/07/2018 10:56:36] System.Private.CoreLib: Exception while executing function: ExecuteWorkItem. test-api: Could not load file or assembly 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.

这是我的函数的签名

public static class ExecuteWorkItem
{
    [FunctionName("ExecuteWorkItem")]
    public static async Task Run(
        [QueueTrigger("work-item")]string queueItem,
        TraceWriter log,
        ExecutionContext executionContext,
        [Inject(typeof(IWorkItemEngine))]IWorkItemEngine workItemEngine,
        [Table("ResultData", Connection = "AzureWebJobsStorage")] CloudTable resultTable,
        [Table("SimulationNodeData", Connection = "AzureWebJobsStorage")] CloudTable simulationNodeTable)
    {}

现在任何想法都会非常感激,唯一似乎有类似问题的人就是这里的这个人:Go to the bottom of his post

他没有回复他的询问,他使用的是 Mac 环境,而我使用的是 Windows 10。

任何帮助将不胜感激。

提前致谢

最佳答案

对于 v2 函数,Function sdk 1.0.14 默认引用 Newtonsoft.Jon v11.0.2,无需显式引用,与 Microsoft.Azure.WebJobs 相同。问题出在你的VS使用的功能核心工具(CLI)上。

在本地调试时检查这行 CLI 输出。

Starting Host (HostId=xx, InstanceId=xxx, Version=2.0.11651.0, ...)

如果您准确地看到 Version=2.0.11651.0,则意味着您的 VS 正在使用旧版本的 CLI。

实际上,Azure Functions 和 Web Jobs Tools 的最新版本(在 VS 菜单上,“工具”>“扩展和更新”)是 15.0.40617.0 。该版本使用最新的CLI(目前主机版本2.0.11888),其中v11 Json.Net得到了很好的支持。更新此扩展可能会解决您的问题。

看来Visual Studio for Mac仍然利用旧的CLI(主机版本2.0.11651),Mac用户可以下载新的功能核心工具using Homebrew并在内容根路径中运行func start。或switch update channel将他们的 VS 更新到最新/预览版本。

关于c# - Azure Functions Newtonsoft.Json 加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51304256/

相关文章:

azure kubernetes 仪表板未配置

c# - 将 json 反序列化为对象 : wrapper class workaround

azure - 在 Azure 中启用 DBServer 审核日志时出现 "CloudException: ... Insufficient read or write permissions on the provided storage account."

.net - 如何在反序列化 JSON 字符串时忽略数组项

c# - 使用 Json.NET 在 Windows Phone 7 中反序列化 JSON

c# - 在 C# 中设置/获取命令行系统变量

c# - 绘图窗口到图像文件

c# - 通过数据验证将小时数输入时间表

c# - 在 xamarin android 中删除 ListView

python - Azure Bing 图像搜索客户端抛出找不到资源的错误