From f9b9c84cf03e25eed761cb649cdcf092e73a83b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Hip=C3=B3lito?= Date: Tue, 15 Jul 2025 13:59:39 +0200 Subject: [PATCH] Fixing Stream variable saved in instance issue (#6785) --- .../WorkflowInstanceStorageDriver.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/modules/Elsa.Workflows.Core/VariableStorageDrivers/WorkflowInstanceStorageDriver.cs b/src/modules/Elsa.Workflows.Core/VariableStorageDrivers/WorkflowInstanceStorageDriver.cs index 566473c3a..bb93720e3 100644 --- a/src/modules/Elsa.Workflows.Core/VariableStorageDrivers/WorkflowInstanceStorageDriver.cs +++ b/src/modules/Elsa.Workflows.Core/VariableStorageDrivers/WorkflowInstanceStorageDriver.cs @@ -4,6 +4,7 @@ using System.Text.Json.Nodes; using Elsa.Expressions.Helpers; using Elsa.Extensions; using JetBrains.Annotations; +using Microsoft.Extensions.Logging; namespace Elsa.Workflows; @@ -12,7 +13,7 @@ namespace Elsa.Workflows; /// [Display(Name = "Workflow Instance")] [UsedImplicitly] -public class WorkflowInstanceStorageDriver(IPayloadSerializer payloadSerializer) : IStorageDriver +public class WorkflowInstanceStorageDriver(IPayloadSerializer payloadSerializer, ILogger logger) : IStorageDriver { /// /// The key used to store the variables in the workflow state. @@ -29,8 +30,18 @@ public class WorkflowInstanceStorageDriver(IPayloadSerializer payloadSerializer) { UpdateVariablesDictionary(context, dictionary => { - var node = JsonSerializer.SerializeToNode(value); - dictionary[id] = node; + try + { + var node = JsonSerializer.SerializeToNode(value); + dictionary[id] = node; + } + catch (Exception ex) when (ex is JsonException or NotSupportedException or ObjectDisposedException) + { + logger.LogWarning(ex, "Failed to serialize variable '{VariableId}' of type '{VariableType}' for workflow instance storage. The variable will be skipped.", + id, value?.GetType().FullName ?? "null"); + + dictionary.Remove(id); + } }); return ValueTask.CompletedTask; }