public override T Execute <T>(CommandConfig config, ICommand <T> command) { ICommandContext context = Context.CommandContext; bool contextReused = false; // We need to check the exception, because the transaction can be in a // rollback state, and some other command is being fired to compensate (eg. decrementing job retries) if (!config.ContextReusePossible || context == null || context.Exception != null) { context = commandContextFactory.CreateCommandContext <T>(command); } else { log.LogDebug($"Valid context found. Reusing it for the current command '{command.GetType().FullName}'"); contextReused = true; context.Reused = true; } try { // Push on stack Context.CommandContext = context; Context.ProcessEngineConfiguration = processEngineConfiguration; return(next.Execute(config, command)); } catch (NullReferenceException e) { context.SetException(e); } catch (Exception e) { context.SetException(e); } finally { try { if (!contextReused) { context.Close(); } } finally { // Pop from stack Context.RemoveCommandContext(); Context.RemoveProcessEngineConfiguration(); Context.RemoveBpmnOverrideContext(); } } return(default);