【问题标题】:Azure Function v2 and system.text.jsonAzure 函数 v2 和 system.text.json
【发布时间】:2019-09-13 13:04:14
【问题描述】:

我正在尝试实现一个使用 .net core 3(预览版 9)作为目标框架并使用新的 System.text.json 命名空间的函数。这是我的代码:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using System.Text.Json;

namespace Backtester
{
    public class Test
    {
        public string Author { get; set; }
        public string Currency { get; set; }
    }

    public static class Function
    {
        [FunctionName("Function")]
        public static void Run([ServiceBusTrigger("%QueueName%", Connection = "AzureWebJobsServiceBus")]string myQueueItem, ILogger log)
        {
            try
            {
                var request = JsonSerializer.Deserialize<Test>(myQueueItem);
                log.LogInformation($"Currency: {request.Currency} - {request.Author}");
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}

当我运行代码并将消息提交到服务总线队列时,该函数被触发但失败并出现以下错误:

[13/09/2019 13:01:25] System.Private.CoreLib: Exception while executing function: Function. Backtester: Could not load file or assembly 'System.Text.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058). Cannot load a reference assembly for execution.
[13/09/2019 13:01:25] MessageReceiver error (Action=UserCallback, ClientId=MessageReceiver1********************, EntityPath=**********, Endpoint=**********************************)
[13/09/2019 13:01:25] System.Private.CoreLib: Exception while executing function: Function. Backtester: Could not load file or assembly 'System.Text.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058). Cannot load a reference assembly for execution.

我得出的结论是,我将不得不将我的项目降级到 .net core 2.2,这将导致大量工作,因为我有一个使用新代码库启动并运行的 Web 项目。

【问题讨论】:

    标签: .net-core-3.0 system.text.json


    【解决方案1】:

    我的解决方案最初是将我的项目降级到 .net core 2.2 并使用 Netwonsoft 的 Json.net(v11 是 Microsoft.AspNetCore.App 和 SignalR.Core 的依赖项)。我要补充一点,json.net 现在是一个成熟得多的产品,所以肯定会从一开始就推荐这条路线。

    从那时起,我实际上已将我的 Azure Function 移至 AWS Lambda,它允许您上传自定义运行时,因此如果您想在云中使用 .net core 3,您可以采用这种方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 2018-12-31
      • 2019-02-26
      相关资源
      最近更新 更多