Skip to content

Commit

Permalink
Fix InvalidCastException on manual workflow restart
Browse files Browse the repository at this point in the history
When restart workflow that starts from any ContentActivity manually InvalidCastException writes to log. It happens because input object deserializes from JSON and contains Dictionary instead of expected ContentEventContext  object.
  • Loading branch information
AndreySurkov committed Jan 16, 2025
1 parent 1814599 commit 83cd06c
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.Localization;
Expand Down Expand Up @@ -53,11 +54,14 @@ public override IEnumerable<Outcome> GetPossibleOutcomes(WorkflowExecutionContex

public override Task OnInputReceivedAsync(WorkflowExecutionContext workflowContext, IDictionary<string, object> input)
{
var contentEvent = input?.GetValue<ContentEventContext>(ContentEventConstants.ContentEventInputKey);

if (contentEvent != null)
if(input != null && input.TryGetValue(ContentEventConstants.ContentEventInputKey, out var contentEventObj))
{
InlineEvent = contentEvent;
InlineEvent = contentEventObj switch
{
ContentEventContext contentEvent => contentEvent,
IDictionary<string, object> contentEventDict => JObject.FromObject(contentEventDict).ToObject<ContentEventContext>(),
_ => throw new InvalidCastException($"Cannot convert {contentEventObj} to ContentEventContext")
};

InlineEvent.IsStart = workflowContext.Status == WorkflowStatus.Starting;
}
Expand Down

0 comments on commit 83cd06c

Please sign in to comment.