public async Task <TriggerResult> TriggerAsync(TriggerParam param) { if (param == null) { throw new ArgumentNullException(nameof(param)); } var entity = param.Instance as IEntityWorkflow; if (entity == null) { // going the non EF way! _logger.LogTrace($@"Processing a non '${param.Instance.Type}' entity instance."); var execution = GetExecution(param.Instance.Type); return(execution.Trigger(param)); } TriggerResult result = null; using (var transaction = _context.Database.BeginTransaction()) { try { Workflow workflow = null; _logger.LogTrace($@"Processing an '${param.Instance.Type}' entity instance with Id = ${entity.Id}."); var execution = GetExecution(param.Instance.Type); await _context.SaveChangesAsync(); // so entity id gets resolved! workflow = FindOrCreate( entity.Id, param.Instance.Type, param.Instance.State, entity.Assignee ); EnsureWorkflowVariables(workflow, param); result = execution.Trigger(param); if (!result.IsAborted) { await PersistWorkflow(workflow, param); await _context.SaveChangesAsync(); transaction.Commit(); } } catch (Exception ex) { transaction.Rollback(); _logger.LogError( ex, $"Error in triggering: {param.Instance.Type}, EntityId: {entity.Id}" ); var transitionContext = new TransitionContext(param.Instance); transitionContext.AddError(ex.ToString()); result = new TriggerResult( param.TriggerName, transitionContext, false ); } } return(result); }