c# - Newtonsoft 11.0.0.0 无法在 Azure Function App 2.0 上加载

标签 c# azure json.net nuget azure-functions

我使用 Azure Http 触发器创建了一个开箱即用的 Azure Function App。这给了我下面的代码。我所更新的只是将 HttpRequest 主体转换为我的 Helper 类。

这是代码

public static class TriggerTest
{
    [FunctionName("TriggerTest")]
    public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log)
    {

        log.Info("C# HTTP trigger function processed a request.");

        string name = req.Query["name"];

        string requestBody = new StreamReader(req.Body).ReadToEnd();

        Helper data = JsonConvert.DeserializeObject<Helper>(requestBody);

        name = name ?? data?.value;

        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
}

public class Helper
{
    public string value { get; set; }
}

当我尝试运行它时,它可以正常编译,但随后控制台会收到以下垃圾邮件

A ScriptHost error has occured

System.Private.CoreLib: Exception while executing function: TriggerTest. TestingAzure.FunctionApp: 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’

所有 Nuget 包都引用 Newtonsoft 11.0.2,这也是 Microsoft.NET.Sdk.Functions 引用的内容。该项目是.NET Standard 2.0 项目。我引用的 Nuget 包是

  • Microsoft.ApplicationInsights v2.7.2
  • Microsoft.Azure.WebJobs.Extensions v3.0.0.-beta8
  • Microsoft.Azure.WebJobs.Extensions.Http v3.0.0-beta8
  • Microsoft.Azure.WebJobs.ServiceBus v3.0.0.-beta5
  • Microsoft-NET-Sdk-Functions v1.0.19 NETStandard、库 v2.0.3
  • Newtonsoft.Json v11.0.2

我正在本地运行此程序,尚未在 Azure 中对其进行测试,但出于测试目的,我需要它在本地工作。

另外,从 Visual Studio 2017 下载的 CLI 是 2.0.1-beta.25

适用于 Visual Studio 的 Azure Functions 和 Web 作业工具版本为 15.10.2009.0

最佳答案

对于 v2 函数,Function sdk 1.0.19(>=1.0.14) 默认引用 Newtonsoft.Jon v11.0.2。该错误是由您的 VS 使用的 CLI 引起的。 2.0.1-beta.25太老了,VS中最新的是2.0.1-beta.38现在。

解决方案是确保下载成功。此外,Microsoft.Azure.WebJobs.ServiceBus 应该是 3.0.0-beta8

  1. 删除旧 CLI 文件夹 %localappdata%\AzureFunctionsTools

  2. 重新启动 VS 并创建新的 Azure 函数。在创建对话框中等待 VS 下载新的 CLI 和模板,直到我们看到提示更改为“更新已准备就绪”。

    enter image description here

    enter image description here

  3. 如果在一段时间后(您这边需要下载 200M 文件)没有看到更新已准备好,请再次检查 %localappdata%\AzureFunctionsTools。如果只有文件夹 2.0.1-beta.25 充满了内容,请尝试重复上述步骤或诉诸 update part of this answer如需手动下载,需要访问%localappdata%\AzureFunctionsTools\feed.json找到最新版本的下载地址(目前feed版本=2.5.2)。

关于c# - Newtonsoft 11.0.0.0 无法在 Azure Function App 2.0 上加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52221820/

相关文章:

c# - Windows Phone 8.1 - 页面导航

c# - 如何使用名为 Configuration 的 MSBuild 命令行参数作为条件编译符号?

azure - 是否可以使用 azure powershell 在 azure 中查看日志警报?

azure - BrokeredMessage Body 与 message.Properties 之间的区别?

c# - 如何从存储在 CouchDB 中的 Serilog 中反序列化 LogEvents

c# - 配置自定义 TextWriterTraceListener 到 web.config

C# WPF MVVM CollectionView 过滤器 - 应用于子 ViewModel

azure - 在 Azure 函数中通过 Nlog 记录到 Logentries

c# - 从 .Net Framework 4.6.1 控制台应用程序转到 Core 2.0 控制台应用程序后,Newtonsoft DeserializeObject 出现错误

c# - 使用 JSON.NET 进行序列化时,使自定义 JsonConverter 尊重 ItemTypeNameHandling