Issue with Azure Durable functions in .NET 8.0 Isolated model returning list of exceptions

Lauris Vonogs 20 Reputation points
2024-07-10T10:31:10.39+00:00

Recently I started upgrading all our Azure Functions from .NET 6.0 In-process model to .NET 8.0 Isolated model and noticed an issue with Durable functions returning list of exceptions, specifically Exceptions that contain innerException (public Exception(string? message, Exception? innerException). It seems the issue could be because the serialization has been changed from Newtonsoft.Json to System.Text.Json based on this page (https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-dotnet-isolated-overview).
So for example, if I try to return a list that contains some exceptions from the Activity Function to the Orchestration Instance, which were created with 2 arguments (message and innerException), the function will fail with this error message ( System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.List1[System.Exception]., and the fail happens when returning the object to the orchestration instance, not on the activity function). And if I try to return it as anonymous type, the function won't fail, but it will return the Name of the object (System.Collections.Generic.List1[System.Exception]), not the data itself. This issue doesn't happen when we try to pass only message (1 argument) to the Exception and it didn't happen also when using .Net 6.0 in-process model.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,930 questions
Developer technologies | .NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Pinaki Ghatak 5,600 Reputation points Microsoft Employee Volunteer Moderator
    2024-07-15T10:50:38.23+00:00

    Hello @Lauris Vonogs

    This issue you mentioned could be related to the change in serialization from Newtonsoft.Json to System.Text.Json, as mentioned in this page (https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-dotnet-isolated-overview). When you try to return a list that contains exceptions with inner exceptions from the Activity Function to the Orchestration Instance, which were created with 2 arguments (message and innerException), the function fails with the error message System.Text.Json.JsonException: The JSON value could not be converted to System.Collections.Generic.List1[System.Exception].

    This issue doesn't happen when you try to pass only message (1 argument) to the Exception and it didn't happen also when using .Net 6.0 in-process model.

    One workaround for this issue is to create a custom serializer that can handle the serialization of exceptions with inner exceptions.

    You can implement the IErrorSerializerSettingsFactory interface to create a custom JsonSerializerSettings object that can handle the serialization of exceptions with inner exceptions. Here is an example of how to implement this interface:

    internal class CustomErrorSerializerSettingsFactory : IErrorSerializerSettingsFactory
    {
    	public JsonSerializerSettings CreateJsonSerializerSettings()
    	{ 
    		var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, 	Converters = { new ExceptionConverter() } }; 
    		return settings;
    	}
    }
    

    In this example, the TypeNameHandling property is set to All to include type information in the serialized JSON. The Converters property is set to an instance of the ExceptionConverter class, which is responsible for handling the serialization of exceptions with inner exceptions.

    You can then register this custom serializer in your startup code like this:

    builder.Services.AddSingleton();
    

    This will ensure that the custom serializer is used for all Durable Functions in your application.

    I hope this helps


    I hope that this response has addressed your query and helped you overcome your challenges. If so, please mark this response as Answered. This will not only acknowledge our efforts, but also assist other community members who may be looking for similar solutions.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.