Azure-functions-host: JsonResult with JsonSerializerSettings throws exception in .NET Core 3.1 / Azure Functions v3

Created on 12 Dec 2019  路  7Comments  路  Source: Azure/azure-functions-host

Issue

AF returning JsonResult with JsonSerializerSettings (StringEnumConverter, NullValueHandling = NullValueHandling.Ignore) instance passed through its constructor throws an exception.

Investigative information

  • Function App version: 3.0
  • Function App runtime: .Net Core 3.1

Repro steps

https://github.com/HMoen/FunctionApp-JsonResult-Error/

Expected behavior

{
    "Status": "Pending",
    "WithValue": "Has value"
}

Actual behavior

Throws exception: Microsoft.AspNetCore.Mvc.NewtonsoftJson: Property 'JsonResult.SerializerSettings' must be an instance of type 'Newtonsoft.Json.JsonSerializerSettings'
Removing the passed JsonSerializerSettings in JsonResult:

{
    "status": 0,
    "withValue": "Has value",
    "nullValue": null
}

Known workarounds

  1. Add Nuget Microsoft.AspNetCore.Mvc.NewtonsoftJson to your poject
  2. Add the following in local.settings.json and Azure App Settings: "FUNCTIONS_V2_COMPATIBILITY_MODE": true

Related information

https://github.com/Azure/azure-functions-host/issues/5358#issue-535397372

Most helpful comment

@fabiocav, one a side note, is there an example in the docs on how to use System.Text.Json with a .NET Core 3.1 AF?

The JsonResult class' SerializerSettings property has the note "When using System.Text.Json, this should be an instance of JsonSerializerOptions", but even without adding the nuget Microsoft.AspNetCore.Mvc.NewtonsoftJson, the JsonResult ctor still requires JsonSerializerSettings and not JsonSerializerOptions.

You can confirm this in the repo https://github.com/HMoen/FunctionApp-JsonResult-Error. That's stopping us from moving to System.Text.Json.

All 7 comments

@HMoen is this happening locally and hosted? If you see this in the hosted environment, can you confirm the actual runtime version you're currently running against?

@fabiocav, it is happening locally and hosted.

Hosted info:

  • Runtime: 3.0.12930.0 (~3)
  • Location: East US
  • Platform: App Service Plan (S1: 1)
  • OS: Windows

Thanks

Thank you for confirming...we have an update going out at the moment addressing an issue that should resolve what you're seeing. That is in 3.0.12939, and currently in West Central US, West US2, North Central US and a few stamps across different regions. We're hoping to have global deployment completed by end of day, with tools likely this coming Monday.

@fabiocav, thanks for the update. I will keep an eye out for the update in my region and will close this thread then.

@fabiocav, one a side note, is there an example in the docs on how to use System.Text.Json with a .NET Core 3.1 AF?

The JsonResult class' SerializerSettings property has the note "When using System.Text.Json, this should be an instance of JsonSerializerOptions", but even without adding the nuget Microsoft.AspNetCore.Mvc.NewtonsoftJson, the JsonResult ctor still requires JsonSerializerSettings and not JsonSerializerOptions.

You can confirm this in the repo https://github.com/HMoen/FunctionApp-JsonResult-Error. That's stopping us from moving to System.Text.Json.

Hi guys...

I have the "same" issue when using JsonConverter(typeof(StringEnumConverter)) data annotation on my classes... But no exceptions throwing and the problem appears at JsonResult, OkContentResult, etc... Everywhere I return a object, my enums properties is serialized as int.

SO... I made some tests and the problem not happens when I DESERIALIZE the objects just at results...

I solved the problem setting the FUNCTIONS_V2_COMPATIBILITY_MODE to true as mentioned here earlier.

It worked localy and remote (South Central US, Consumption, Premium and App Service plan [B1]).

Tks!

@fabiocav, I can confirm function runtime version 3.0.12930.0 solves this issue both locally and hosted. Thanks for the assist.

If you can steer me in the right direction as far as https://github.com/Azure/azure-functions-host/issues/5376#issuecomment-565568023, it'd be much appreciated.

Was this page helpful?
0 / 5 - 0 ratings