Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cvijayak opened this issue May 9, 2024 · 7 comments
Closed
Labels
bug Something isn't working

Comments

@cvijayak
Copy link
Contributor

cvijayak commented May 9, 2024

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 cvijayak added the bug Something isn't working label May 9, 2024
Copy link

glime-ai bot commented May 9, 2024

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.

@cvijayak
Copy link
Contributor Author

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.

@sfmskywalker
Copy link
Member

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

@cvijayak
Copy link
Contributor Author

@sfmskywalker
I think the changes in the commit below will resolve this bug. Please share your comments on this.
367f574

@cvijayak
Copy link
Contributor Author

'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

@sfmskywalker
Copy link
Member

@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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

No branches or pull requests

2 participants