Skip to content

Commit

Permalink
Avoid null reference exception (#15420)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Mar 3, 2024
1 parent aa68c6c commit 24e9b7d
Showing 1 changed file with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ public class WorkflowTypeStep : IRecipeStepHandler
{
private readonly IWorkflowTypeStore _workflowTypeStore;
private readonly ISecurityTokenService _securityTokenService;
private readonly IActionContextAccessor _actionContextAccessor;
private readonly IUrlHelperFactory _urlHelperFactory;
private readonly JsonSerializerOptions _jsonSerializerOptions;
private readonly IUrlHelper _urlHelper;

public WorkflowTypeStep(IWorkflowTypeStore workflowTypeStore,
ISecurityTokenService securityTokenService,
IActionContextAccessor actionContextAccessor,
IOptions<JsonSerializerOptions> jsonSerializerOptions,
IUrlHelperFactory urlHelperFactory)
IUrlHelperFactory urlHelperFactory,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_workflowTypeStore = workflowTypeStore;
_securityTokenService = securityTokenService;
_actionContextAccessor = actionContextAccessor;
_urlHelperFactory = urlHelperFactory;
_jsonSerializerOptions = jsonSerializerOptions.Value;
_urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
}

public async Task ExecuteAsync(RecipeExecutionContext context)
Expand All @@ -44,25 +46,30 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
}

var model = context.Step.ToObject<WorkflowStepModel>();
var urlHelper = GetUrlHelper();

foreach (var token in model.Data.Cast<JsonObject>())
{
var workflow = token.ToObject<WorkflowType>(_jsonSerializerOptions);

foreach (var activity in workflow.Activities.Where(a => a.Name == nameof(HttpRequestEvent)))
{
var tokenLifeSpan = activity.Properties["TokenLifeSpan"];
if (tokenLifeSpan != null)
{
activity.Properties["Url"] = ReGenerateHttpRequestEventUrl(workflow, activity, tokenLifeSpan.ToObject<int>());
}
}

var existing = await _workflowTypeStore.GetAsync(workflow.WorkflowTypeId);

if (existing == null)
if (existing is null)
{
workflow.Id = 0;

if (urlHelper is not null)
{
foreach (var activity in workflow.Activities.Where(a => a.Name == nameof(HttpRequestEvent)))
{
if (!activity.Properties.TryGetPropertyValue("TokenLifeSpan", out var tokenLifeSpan))
{
continue;
}

activity.Properties["Url"] = ReGenerateHttpRequestEventUrl(urlHelper, workflow, activity, tokenLifeSpan.ToObject<int>());
}
}
}
else
{
Expand All @@ -73,12 +80,25 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
}
}

private string ReGenerateHttpRequestEventUrl(WorkflowType workflow, ActivityRecord activity, int tokenLifeSpan)
private IUrlHelper _urlHelper;

private IUrlHelper GetUrlHelper()
{
// When 'UrlHelper' is instantiated outside a controller's action (e.g., in a BackgroundTask), the ActionContext is null.
if (_urlHelper is null && _actionContextAccessor.ActionContext is not null)
{
_urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);
}

return _urlHelper;
}

private string ReGenerateHttpRequestEventUrl(IUrlHelper urlHelper, WorkflowType workflow, ActivityRecord activity, int tokenLifeSpan)
{
var token = _securityTokenService.CreateToken(new WorkflowPayload(workflow.WorkflowTypeId, activity.ActivityId),
TimeSpan.FromDays(tokenLifeSpan == 0 ? HttpWorkflowController.NoExpiryTokenLifespan : tokenLifeSpan));

return _urlHelper.Action("Invoke", "HttpWorkflow", new { token });
return urlHelper.Action("Invoke", "HttpWorkflow", new { token });
}
}

Expand Down

0 comments on commit 24e9b7d

Please sign in to comment.