azure-functions-dotnet-worker: Unable to override the JsonSerializer when using ConfigureFunctionsWebApplication
Hi,
I migrated from In process to .NET 8 Worker with AspNetCore integration and can’t set the serializer to be Newtonsoft so I can keep old behavior for [FromBody] attribute.
What I’ve tried:
Option 1:
.ConfigureFunctionsWebApplication(workerAppBuilder => workerAppBuilder.UseNewtonsoftJson())
Option 2:
.ConfigureServices((context, services) =>{
services.Configure<WorkerOptions>(workerOptions =>
{
JsonSerializerSettings settings = NewtonsoftJsonObjectSerializer.CreateJsonSerializerSettings();
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
settings.NullValueHandling = NullValueHandling.Ignore;
workerOptions.Serializer = new NewtonsoftJsonObjectSerializer(settings);
})
}|)
The exception I am getting is:
[2023-12-06T09:35:04.528Z] Function 'createOrUpdate', Invocation id '172f2590-9810-48b0-8fa7-bdb1b02b1ea6': An exception was thrown by the invocation.
[2023-12-06T09:35:04.530Z] Result: Function 'createOrUpdate', Invocation id '172f2590-9810-48b0-8fa7-bdb1b02b1ea6': An exception was thrown by the invocation.
Exception: System.InvalidOperationException: The JSON value could not be converted to System.Nullable`1[System.Guid]. Path: $.repeatIntervalUnitsId | LineNumber: 0 | BytePositionInLine: 27.
[2023-12-06T09:35:04.534Z]
[2023-12-06T09:35:04.536Z] at Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore.FromBodyConverstionFeature.ConvertAsync(FunctionContext context, Type targetType) in D:\a\_work\1\s\extensions\Worker.Extensions.Http.AspNetCore\src\FromBodyConverstionFeature.cs:line 91
[2023-12-06T09:35:04.537Z] at Microsoft.Azure.Functions.Worker.Extensions.Http.Converters.FromBodyConverter.HandleResultAsync(ValueTask`1 result) in D:\a\_work\1\s\extensions\Worker.Extensions.Http\src\FromBodyConverter.cs:line 28
[2023-12-06T09:35:04.548Z] at Microsoft.Azure.Functions.Worker.Context.Features.DefaultInputConversionFeature.AwaitAndReturnConversionTaskResult(ValueTask`1 conversionTask) in D:\a\_work\1\s\src\DotNetWorker.Core\Context\Features\DefaultInputConversionFeature.cs:line 103
[2023-12-06T09:35:04.549Z] Executed 'Functions.createOrUpdate' (Failed, Id=172f2590-9810-48b0-8fa7-bdb1b02b1ea6, Duration=343ms)
[2023-12-06T09:35:04.555Z] at Microsoft.Azure.Functions.Worker.Context.Features.DefaultInputConversionFeature.ConvertAsync(ConverterContext converterContext) in D:\a\_work\1\s\src\DotNetWorker.Core\Context\Features\DefaultInputConversionFeature.cs:line 43[2023-12-06T09:35:04.556Z] System.Private.CoreLib: Exception while executing function: Functions.createOrUpdateTask. System.Private.CoreLib: Result: Failure
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 23 (5 by maintainers)
I’m keen to see an answer on this as well as NewtonsoftJson is required for the SendGrid integration to work correctly.
Anyone?
@davidpetric we’ll take a closer look at your issue before providing any additional guidance, as the requirement doesn’t seem right.
@simonholmandev can you please open a separate issue for the SendGrid problem (with the stack/details) so we can take a closer look at that? Thanks!
Setting custom serializer this way doesn’t work for Cosmos bindings either
@liliankasem the issue is not only with [FromBody] but also if you use
return new OkObjectResult(myCustomObject);
then also the System.Text.Json serializer is used.
i opened https://github.com/MicrosoftDocs/azure-docs/issues/122534 but there is nothing happening.
What is going on here? Why is this not fixed ?
Using the ConfigureFunctionsWebApplication is the only way of dealing with http triggered functions.
Do you really want us to use the Microsoft json serializer? This is impossible!
We run from one problem to the next while changing to isolated-worker model. Which we are required to use if we ever want to upgrade to .net 8
Just fix this bug !
Im just going to move away from Newtonsoft with isolated worker, looks like the push is to System.Text.Json, who knows what will come. The v4 cosmosdb worker package includes NewtonSoft.json, that should be taken out of there if its intended to be opt in, why have it as a dependency? And im starting wonder if there is any real benefit in even using Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore, that makes you use ConfigureFunctionsWebApplication, which is were all the confusion is.
Hey,
I was wondering if you have any updates on whether this is a safe or recommended way of doing it.
Thank you
Was able to make the error go away:
Install Nuget:
Microsoft.AspNetCore.Mvc.NewtonsoftJsonAdd
services.AddMvcCore().AddNewtonsoftJson();inConfigureServices()But is this the right way of doing it? I am wondering if this will create issues…