Azure / azure-functions-host

The host/runtime that powers Azure Functions
https://functions.azure.com
MIT License
1.94k stars 442 forks source link

Azure Function V2 and System.Text.Json #4925

Closed liamfl closed 5 years ago

liamfl commented 5 years ago

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.

ladeak commented 5 years ago

With netcoreapp2.2 I am getting the following exception:

System.MissingMethodException: 'Method not found: 'Int32 System.Text.Encodings.Web.TextEncoder.FindFirstCharacterToEncodeUtf8(System.ReadOnlySpan`1<Byte>)'.'

Stack:

at System.Text.Json.JsonWriterHelper.NeedsEscaping(ReadOnlySpan1 value, JavaScriptEncoder encoder) at System.Text.Json.JsonPropertyInfo.DeterminePropertyName() at System.Text.Json.JsonPropertyInfo.GetPolicies() at System.Text.Json.JsonPropertyInfoCommon4.Initialize(Type parentClassType, Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type elementType, JsonConverter converter, JsonSerializerOptions options) at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonConverter converter, JsonSerializerOptions options) at System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options) at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options) at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType) at System.Text.Json.JsonSerializer.ReadValueCore(Utf8JsonReader& reader, Type returnType, JsonSerializerOptions options) at System.Text.Json.JsonSerializer.Deserialize[TValue](Utf8JsonReader& reader, JsonSerializerOptions options) at IotProcessor.IoTHubProcessorFunction.Deserialize(ArraySegment`1 data) in C:[]path\Function.cs:line 53

Repro

var bytes = Encoding.UTF8.GetBytes("{\"R\":\"current\", \"T\": 22.9}");
var d = JsonSerializer.Deserialize<DeviceRequestMessage>(bytes.AsSpan());
andreasohlund commented 5 years ago

Same here, getting:

[2019-10-06 19:15:23] System.Private.CoreLib: Exception while executing function: IndexNuGetPackages. PublicAPI.Functions: Could not load file or assembly 'System.Text.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.

Targeting netcoreapp3.0 in my csproj

andreasohlund commented 5 years ago

Seems like Azure Functions doesn't support netcore 3.0 yet https://github.com/Azure/app-service-announcements/issues/200 so that seems like the likely explanation :)

fabiocav commented 5 years ago

@andreasohlund is correct. This is expected until you're running against the runtime tracked by the announcement made here: Azure/app-service-announcements#200

Please continue to follow that announcement for updates.