elsa-workflows / elsa-workflows/elsa-core

[BUG] Returning Null from Elsa Mediator handler SendAsync throws InvalidCastException

Open
#5,395 1 comment 0 reactions 0 assignees View on GitHub
bug triaged
Dominant language
C#
Stars
7.9k
Forks
1.5k
Avg merge
15h 22m
Merged PRs (30d)
114

Description

## Description
If you return null from an Elsa Mediator handler (even when the return type is nullable) the DefaultMediator.SendAsync method throws and InvalidCastException.

## Steps to Reproduce

**Detailed Steps**:
Create a Elsa Mediator IRequestHandler for a Mediator Request `TRequest`, which returns a type `TResponse?`, return null at any point in the handler and you should get the exception, even though null is a valid response type

2. **Code Snippets**:

`
public sealed class GetWorkflowDefinitionHandler : IRequestHandler
{
// This is the Elsa provided Elsa.Workflows.Management.Contracts store
private readonly IWorkflowDefinitionStore _workflowDefinitionStore;

public GetWorkflowDefinitionHandler(IWorkflowDefinitionStore workflowDefinitionStore)
{
_workflowDefinitionStore = workflowDefinitionStore;
}

public async Task HandleAsync(GetWorkflowDefinition request, CancellationToken cancellationToken)
{
// Workflow definition can return as Null here, when it does SendAsync throws an error
return await _workflowDefinitionStore.FindAsync(
new WorkflowDefinitionFilter { DefinitionId = request.DefinitionId, VersionOptions = VersionOptions.Latest },
cancellationToken);
}
}`

4. **Reproduction Rate**:
Every time null is returned from the Handler

## Expected Behavior
Null should be returned fine

## Actual Behavior
An invalid cast exception is thrown

## Screenshots
If possible, add screenshots or screen recordings to help explain the problem.

## Environment
- **Elsa Package Version**:
Elsa.Workflows.Core = 3.1.2

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.