Azure Function v2和system.text.json [英] Azure Function v2 and system.text.json
问题描述
我正在尝试实现一个功能,该功能使用.net core 3(预览版9)作为目标框架并使用新的System.text.json命名空间.这是我的代码:
I am trying to implement a function that uses .net core 3 (preview 9) as a target framework and uses the new System.text.json namespace. Here is my code:
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;
}
}
}
}
当我运行代码并将一条消息提交到服务总线队列时,该函数被触发,但失败,并出现以下错误:
When I run the code and submit a message onto the service bus queue, the function is triggered but fails with the following error:
[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项目.
I'm coming to the conclusion that I will have to downgrade my project to .net core 2.2, which will cause a fair amount of work as I have a web project up and running using the new codebase.
推荐答案
我的解决方案最初是将项目降级为.net core 2.2,并使用Netwonsoft的Json.net(v11是Microsoft.AspNetCore.App和无论如何,SignalR.Core).我会补充说json.net现在是一种更加成熟的产品,因此绝对会从一开始就推荐这条路线.
My solution to this was initially to downgrade my project to .net core 2.2 and use Netwonsoft's Json.net (v11 is a dependency of Microsoft.AspNetCore.App and SignalR.Core anyway). I would add that json.net is a far more mature product right now so would definitely recommend this route from the start.
从那时起,我实际上将我的Azure功能移到了AWS Lambda,它确实允许您上载自定义运行时,因此,如果您想在云中使用.net core 3,可以采用这种方式.
Since then I have actually moved my Azure Function over to AWS Lambda, which does allow you to upload a custom runtime so if you want to use .net core 3 in the cloud you could go this way.
这篇关于Azure Function v2和system.text.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!