elsa-workflows / elsa-workflows/elsa-core
[BUG] ElasticSearch - Executing a workflow with an error results in a Serialization and deserialization of 'System.Type' instances is not supported ($.WorkflowState.Incidents.Exception.Type)
- Dominant language
- C#
- Stars
- 7.9k
- Forks
- 1.5k
- Avg merge
- 15h 22m
- Merged PRs (30d)
- 114
Description
## Description
I've created a workflow which looks as follows:

When I execute it I end up with the following error:
Elastic.Transport.UnexpectedTransportException: Serialization and deserialization of 'System.Type' instances is not supported. Path: $.WorkflowState.Incidents.Exception.Type.
---> System.NotSupportedException: Serialization and deserialization of 'System.Type' instances is not supported. Path: $.WorkflowState.Incidents.Exception.Type.
This only occurs with Workflow instances which have errors.
## Steps to Reproduce
To help us identify the issue more quickly, please follow these guidelines:
1. **Detailed Steps**: Provide a step-by-step description of what leads to the bug. Be as specific as possible.
- Create a workflow with a fault Activity. Execute it and the error occurs.
2. **Code Snippets**: If the issue involves code (e.g., JavaScript error, server request failure), include the relevant snippets where the issue occurs.
- This error occurs in WorkflowInstanceStore.cs
```c
public async ValueTask SaveAsync(WorkflowInstance instance, CancellationToken cancellationToken = default) =>
await _store.SaveAsync(instance, cancellationToken);
```
3. **Attachments**:
- **Workflow JSON**: If your issue is reproducible with a specific workflow, please attach the workflow's JSON file. This will help us understand the exact process that leads to the problem.
- **Sample Project**: If possible, attach a minimal sample project or code that reproduces the issue. This could be a simplified version of your project that isolates and demonstrates the bug.
4. **Reproduction Rate**: Indicate how often the bug occurs when following these steps (e.g., "every time", "intermittently: about 50% of the time").
- Every Time
5. **Video/Screenshots**: Sometimes, complex behaviors are better shown than described. If you think a video or screenshots would help clarify the issue, please include them.
- None
6. **Additional Configuration**:
```c
elsa.UseWorkflowManagement(management =>
{
// Use EF core for workflow definitions and instances.
management.UseWorkflowDefinitions(ef => ef.UseEntityFrameworkCore(ef => ef.UseSqlServer(connectionString)));
management.UseWorkflowInstances(i => i.UseElasticsearch());
});
elsa.UseElasticsearch(options =>
{
builder.Configuration.GetSection("Elsa:Elasticsearch").Bind(options);
var indexNameMappingSettings = builder.Configuration.GetSection("Elsa:Elasticsearch:IndexNameMappings").GetChildren();
foreach (var indexNameMappingSetting in indexNameMappingSettings)
{
var entityType = Type.GetType(indexNameMappingSetting.Key);
if (entityType == null)
continue;
options.IndexNameMappings[entityType] = indexNameMappingSetting.Value;
}
});
```
## Expected Behavior
Describe what you expected to happen.
- I should get an entry in the ElasticSearch WorkflowInstance index.
## Actual Behavior
Describe what actually happened. Include screenshots, if applicable.
- I get the error described above with no entry in the ElasticSearch Index.
## Screenshots
If possible, add screenshots or screen recordings to help explain the problem.

## Environment
- I've cloned the 3.1.3 branch.
- Mac ( 14.0 (23A344 )
## Troubleshooting Attempts
I've created the Workflow with a fault to recreate this issue.
## Possible fix
I was considering creating a CustomConverter like the following:
```c
public class CustomJsonConverterForType : JsonConverter
{
public override Type Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
// Caution: Deserialization of type instances like this
// is not recommended and should be avoided
// since it can lead to potential security issues.
// If you really want this supported (for instance if the JSON input is trusted):
// string assemblyQualifiedName = reader.GetString();
// return Type.GetType(assemblyQualifiedName);
throw new NotSupportedException();
}
public override void Write(
Utf8JsonWriter writer,
Type value,
JsonSerializerOptions options
)
{
string assemblyQualifiedName = value.AssemblyQualifiedName;
// Use this with caution, since you are disclosing type information.
writer.WriteStringValue(assemblyQualifiedName);
}
}
```
I'm not sure if you have any other suggestions?
Contributor guide
Assessment
This issue has not been assessed yet.