问题
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.
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.
回答1:
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.
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.
来源:https://stackoverflow.com/questions/57924191/azure-function-v2-and-system-text-json