c# - Newtonsoft.Json 无法在本地 Azure Functions 应用程序中加载

标签 c# json.net azure-functions

我正在开发一个 Azure Functions“应用程序”,我在本地进行大部分测试,然后将 DLL 发布到 Azure。我从 3 月份开始,直到几周前它一直运行良好。

我主要使用 macOS High Sierra(目前是最新版本;10.13.4)和 Visual Studio 在稳定版上进行开发和测试。

我最近在提示我更新 Visual Studio 时进行了更新,它似乎已更新为支持 .NETStandard 2.0 而不是 .NET Framework 4.7。*,但我的项目仍保留在 .NET Framework 4.7 上。然而,当我尝试通过对管理端点的 POST 请求运行我的计时器触发器时,出现了一个错误,告诉我应用程序无法加载 Newtonsoft.Json。

我为此苦苦挣扎了一段时间,最终使用 .NETStandard 2.0 创建了一个新项目并复制了文件,但同样的错误仍然存​​在。

Newtonsoft.Json 从 Visual Studio for Mac 的内置 NuGet 包管理器安装。

我还在 Windows(Windows Server 2012 R2 Standard)上测试了同一个项目,但我遇到了完全相同的错误。

我得到的错误:

[06/05/2018 08:17:18] System.Private.CoreLib: Exception while executing function: Posts. MyAzureFunctionsApp: 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'.
[06/05/2018 08:17:18] 
[06/05/2018 08:17:18] Executed 'Posts' (Failed, Id=7c288cdf-7807-4450-92cd-c64370e241a5)
[06/05/2018 08:17:18] System.Private.CoreLib: Exception while executing function: Posts. MyAzureFunctionsApp: 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'.

Newtonsoft.Json 版本 11.0.0 不存在,它当前的版本为 11.0.2,DLL 声明版本为 11.0.2.21924,因此我尝试了“绑定(bind)重定向”。这似乎也没有任何区别。

app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
      <assemblyIdentity name="Newtonsoft.Json"
                publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
      <bindingRedirect oldVersion="11.0.0.0" newVersion="11.0.2.21924"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

CSProj 文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.13" />
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

我清理了这个项目十几次,创建了一个全新的项目,删除了 ~/.nuget,重新安装了 Visual Studio 并在 Windows 上测试了同一个项目。但我每次仍然遇到同样的错误。

最佳答案

尚不支持绑定(bind)重定向 ( GitHub issue )。

当以 netstandard2.0 ( NuGet package ) 为目标时,Microsoft.NET.Sdk.Functions 1.0.13 将 Newtonsoft.Json 10.0.3 作为依赖项。

关于c# - Newtonsoft.Json 无法在本地 Azure Functions 应用程序中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50695738/

相关文章:

c# - 绑定(bind)中的Azure函数表存储违反了类型参数 'TElement'的约束

azure - 如何在 Azure(而不是 AWS 亚马逊)上构建无服务器网站

c# - 将定制的 Linq-2-SQL 迁移到 Entity Framework

c# - 使用c#提高dacpac部署的性能

c# - 如何在 C# 中使用 “for” 循环创建动态递增变量

c# - 在 JSON.NET 中反序列化此对象

在 Azure Function 中运行 R.NET

c# - 提高 IEnumerable<byte> 解析性能

c# - 使用 NewtonSoft 解析 JSON 时出错

c# - 自定义集合属性未序列化