elsa-core icon indicating copy to clipboard operation
elsa-core copied to clipboard

[BUG] Serialization error when 'MessageReceived' activity is used in workflow

Open cvijayak opened this issue 1 year ago • 4 comments

Description

Following is the class created for the message 'OrderCompleted'.

    public class OrderCompleted : IMessage
    {
        public string OrderId { get; set; }
    }

    public interface IMessage { }

I am able to publish the message in Azure Service Bus using MassTransit. However, when the message is received in a workflow that uses the 'MessageReceived' activity, I encounter the following serialization exception.

"Message":"Serialization and deserialization of 'System.Type' instances is not supported.", "Data":{}, "Source":"System.Text.Json", "HResult":-2146233067, "StackTrace":"  
at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter`1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)\r\n   
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n   
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)\r\n   
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)\r\n  
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n  
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)"}, "Source":"System.Text.Json", "HResult":-2146233067, "StackTrace":"   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex)\r\n   
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n   
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)\r\n   
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.SerializeAsObject(Utf8JsonWriter writer, Object rootValue)\r\n   
at System.Text.Json.JsonSerializer.WriteStringAsObject(Object value, JsonTypeInfo jsonTypeInfo)\r\n   
at Elsa.Workflows.Serialization.Converters.PolymorphicObjectConverter.Write(Utf8JsonWriter writer, Object value, JsonSerializerOptions options)\r\n  
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n   
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)\r\n   
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)\r\n  
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n  
at System.Text.Json.Serialization.JsonConverter`1.TryWriteAsObject(Utf8JsonWriter writer, Object value, JsonSerializerOptions options, WriteStack& state)\r\n  
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n   
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)\r\n   
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)\r\n  
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n  
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n  
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)\r\n   
at System.Text.Json.JsonSerializer.Serialize[TValue](Utf8JsonWriter writer, TValue value, JsonSerializerOptions options)\r\n   
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n  
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)\r\n 
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed)\r\n   
at System.Text.Json.JsonSerializer.WriteBytes[TValue](TValue& value, JsonTypeInfo`1 jsonTypeInfo)\r\n   
at MassTransit.Serialization.SystemTextJsonMessageBody`1.GetBytes() in /_/src/MassTransit/Serialization/SystemTextJsonMessageBody.cs:line 50"}, "Source":"MassTransit", "HResult":-2146233076, "StackTrace":"   
at MassTransit.Serialization.SystemTextJsonMessageBody`1.GetBytes() in /_/src/MassTransit/Serialization/SystemTextJsonMessageBody.cs:line 56\r\n  
at MassTransit.AzureServiceBusTransport.ServiceBusSendTransportContext.CreateMessage[T](AzureServiceBusSendContext`1 context) in /_/src/Transports/MassTransit.Azure.ServiceBus.Core/AzureServiceBusTransport/ServiceBusSendTransportContext.cs:line 177\r\n   
at MassTransit.AzureServiceBusTransport.ServiceBusSendTransportContext.Send[T](SendEndpointContext sendEndpointContext, SendContext`1 sendContext)\r\n  
at MassTransit.Transports.SendTransport`1.SendPipe`1.Send(TContext context) in /_/src/MassTransit/Transports/SendTransport.cs:line 93\r\n   
at MassTransit.Transports.SendTransport`1.SendPipe`1.Send(TContext context) in /_/src/MassTransit/Transports/SendTransport.cs:line 111\r\n   
at MassTransit.Agents.PipeContextSupervisor`1.Send(IPipe`1 pipe, CancellationToken cancellationToken) in /_/src/MassTransit/Agents/PipeContextSupervisor.cs:line 55\r\n   
at MassTransit.Agents.PipeContextSupervisor`1.Send(IPipe`1 pipe, CancellationToken cancellationToken) in /_/src/MassTransit/Agents/PipeContextSupervisor.cs:line 61\r\n  
at MassTransit.Agents.PipeContextSupervisor`1.Send(IPipe`1 pipe, CancellationToken cancellationToken) in /_/src/MassTransit/Agents/PipeContextSupervisor.cs:line 67\r\n 
at MassTransit.Transports.HostConfigurationRetryExtensions.Retry(IHostConfiguration hostConfiguration, Func`1 factory, CancellationToken cancellationToken, CancellationToken stoppingToken) in /_/src/MassTransit/Transports/HostConfigurationRetryExtensions.cs:line 39\r\n   
at Elsa.MassTransit.Services.MassTransitWorkflowDispatcher.DispatchAsync(DispatchTriggerWorkflowsRequest request, DispatchWorkflowOptions options, CancellationToken cancellationToken)\r\n   at Elsa.Workflows.Runtime.Services.ValidatingWorkflowDispatcher.DispatchAsync(DispatchTriggerWorkflowsRequest request, DispatchWorkflowOptions options, CancellationToken cancellationToken)\r\n   
at Elsa.MassTransit.Consumers.WorkflowMessageConsumer`1.Consume(ConsumeContext`1 context)\r\n   
at MassTransit.DependencyInjection.ScopeConsumerFactory`1.Send[TMessage](ConsumeContext`1 context, IPipe`1 next) in /_/src/MassTransit/DependencyInjection/DependencyInjection/ScopeConsumerFactory.cs:line 22\r\n   
at MassTransit.DependencyInjection.ScopeConsumerFactory`1.Send[TMessage](ConsumeContext`1 context, IPipe`1 next) in /_/src/MassTransit/DependencyInjection/DependencyInjection/ScopeConsumerFactory.cs:line 22\r\n   
at MassTransit.Middleware.ConsumerMessageFilter`2.MassTransit.IFilter<MassTransit.ConsumeContext<TMessage>>.Send(ConsumeContext`1 context, IPipe`1 next) in /_/src/MassTransit/Middleware/ConsumerMessageFilter.cs:line 48"}

Expected Behavior

The 'MessageReceived' activity should be able to receive the message 'OrderCompleted' without any issues.

Actual Behavior

The 'MessageReceived' activity throws a serialization error when the message 'OrderCompleted' arrives from Azure Service Bus.

Screenshots

Screenshot shows the error from Azure Service Bus

image

Additional Context

Below is the 'MessageReceived' activity configuration from WorkflowDefinition

image

Below screenshot shows how the dashboard shows the masstransit activities

image

I am using elsa 3.1.3

cvijayak avatar May 09 '24 07:05 cvijayak

It appears that you are encountering a serialization issue with the MessageReceived activity in Elsa when using MassTransit with Azure Service Bus. This issue is related to the serialization and deserialization of System.Type instances, which is not supported by System.Text.Json, the default serializer used by Elsa.

To resolve this issue, you can switch to using Newtonsoft.Json for serialization, which does support serializing System.Type instances. You can configure Elsa to use Newtonsoft.Json by adding the appropriate package and configuring the serialization options.

Here are the steps you can follow:

  1. Install the Elsa.NewtonsoftJson NuGet package.
  2. Configure Elsa to use Newtonsoft.Json by modifying the service registration code in your Startup.cs or Program.cs file, depending on your .NET version.

Here's an example of how you might configure the services:

services
    .AddElsa(elsa => elsa
        .UseEntityFrameworkPersistence(ef => ef.UseSqlite(...))
        .AddConsoleActivities()
        .AddHttpActivities()
        .AddMassTransitActivities()
        .UseNewtonsoftJson() // Use Newtonsoft.Json for serialization
    );

Make sure to replace the ... with your actual Entity Framework configuration or other Elsa configuration as needed.

By making this change, you should be able to avoid the serialization issue you're encountering with System.Type instances.

Please note that the links to the screenshots you provided are not accessible, but the steps above should guide you in resolving the serialization issue based on the error message and the related GitHub issues you've referenced.

This comment was generated by Glime.

glime-ai[bot] avatar May 09 '24 07:05 glime-ai[bot]

The suggested solution by AI will not work because Elsa 3 does not have the NuGet package 'Elsa.NewtonsoftJson,' as far as I understand.

Am I missing anything in the 'MessageReceived' activity configuration in the WorkflowDefinition? Please confirm.

cvijayak avatar May 10 '24 04:05 cvijayak

Looks like a bug. NewtonsoftJson has indeed nothing to do with it.

sfmskywalker avatar May 10 '24 13:05 sfmskywalker

@sfmskywalker I think the changes in the commit below will resolve this bug. Please share your comments on this. https://github.com/elsa-workflows/elsa-core/pull/5352/commits/367f5749e8d5676ae10de5abb2003b4781ef6fba

cvijayak avatar May 13 '24 05:05 cvijayak

'MessageReceived' activity is working well in the latest code on the main branch.

I downloaded the latest Elsa code from the main branch and checked it with my changes. The 'Message Received' activity is receiving messages from 'Azure Service Bus' without any problem with serialization.

I am not sure which change solved the issue. Please let me know if someone knows the commit that solves the issue.

I believe these changes will be included in version 3.2, which is scheduled for release on June 1, 2024.

Please confirm the same

cvijayak avatar May 15 '24 04:05 cvijayak

@cvijayak your guess is as good as mine, my friend. And yes, the code in the main branch will be released into 3.2.

sfmskywalker avatar May 17 '24 16:05 sfmskywalker

Closing this issue, given that this doesn't seem to be reproducible with the source code version,

sfmskywalker avatar May 17 '24 16:05 sfmskywalker

I'm having this issue. Any help with the solution? I'm new to Elsa Workflow and I'm using 3.1

LenProg avatar Jul 25 '24 18:07 LenProg

This should be fixed as of 3.2 (3.2-rc3 is available via nuget in case you want to try).

sfmskywalker avatar Jul 25 '24 19:07 sfmskywalker