Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
432 stars 184 forks source link

JObject Serialization issues in Azure Functions Isolated process with Service Bus connection #2632

Open AnnaGolosova opened 3 months ago

AnnaGolosova commented 3 months ago

What version of .NET does your existing project use?

.NET 6

What version of .NET are you attempting to target?

.NET 8

Description

Hi all! I'm working on migration from Azure Function on .NET 6 in-process model to .NET 8 isolated process. I have Service Bus queue trigger to listen queueA and after processing I want to put message to other queue queueB.

I have code like this:

[Function(nameof(HandleQueueA))]
[ServiceBusOutput(OutputQueueB, Connection = "ServiceBusConnection")]
public async Task<Envelope<JObject>> Run(
    [ServiceBusTrigger(StartQueueA, Connection = "ServiceBusConnection")]
    ServiceBusReceivedMessage message)
{
    var eventData = JsonConvert.DeserializeObject<Envelope<JToken>>(message.Body);

    // some logic here

    return eventData;
}

Definition for Envelope class is like:

public class Envelope<T>
 {
     public string Id { get; set; }

     public DateTimeEpoch CreatedAt { get; set; }

     public string EventInstance { get; set; }

     public T Content { get; set; }
 }

In JObject content here I have some business attributed for event itself. And this works with no errors but at the end in queueB I can see not fully serialized message like:

{
  "Id": "<some-guid>",
  "CreatedAt": {
    "DateTime": "2024-07-30T15:14:38.5585185Z",
    "Epoch": 1722352478
  },
  "EventInstance": "<some-guid>",
  "Content": {
    "attribute1": [],
    "attribute2": [],
    "some-collection1": [
      [
        []
      ],
      [
        []
      ]
    ],
    "collection2": [
      [
        []
      ],
      [
        []
      ]
    ]
  }
}

If I'm trying to serialize eventData before returning from function, I can see correct data

If using Newtonsoft.Json.JsonConvert.SerializeObject(eventData):

"{\"Id\":\"<some-guid>\",\"createdAt\":{\"dateTime\":\"2024-07-30T15:40:12.5198266Z\",\"epoch\":1722354012},\"content\":{\"attribute1\":\"attribute1Value\",\"attribute2\":\"attribute2Value\",\"some-collection1\":{\"name\":\"name\",\"value\":\"value\"},\"collection2\":{\"name\":\"name\",\"value\":\"value\"} I have the next global serialization settings:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
    DefaultValueHandling = DefaultValueHandling.Include,
    NullValueHandling = NullValueHandling.Ignore,
    ContractResolver = new CamelCasePropertyNamesContractResolver(),
    Converters = new List<JsonConverter> { new StringEnumConverter(), new IsoDateTimeConverter() },
};

I would appreviate any ideas what's wrong here :)

Thanks in advance!

Project configuration and dependencies

.NET 8 Microsoft.Azure.Functions.Worker 1.22.0 Microsoft.Azure.Functions.Worker.Extensions.ServiceBus 5.20.0 Microsoft.Azure.Functions.Worker.Sdk 1.17.4

Link to a repository that reproduces the issue

No response

sravanreddy6745 commented 2 months ago

Can you share the JToken Model

AnnaGolosova commented 2 months ago

This is JToken class from Newtonsoft.Json nuget package: https://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jtoken.htm