private void SetApplicationCommandResult(SagaTransactionDomainNotification notification)
        {
            var applicationCommandId   = notification.ApplicationCommandId;
            var applicationCommandType = notification.ApplicationCommandType;

            const string prefix = "Received the saga domain notification of application Command";

            if (!ExecutingPromiseDict.TryRemove(applicationCommandId, out var executionPromise))
            {
                _logger.LogWarning(FormatReplyMessage(notification, prefix, "but task source does not exist"));

                return;
            }

            var replyScheme = executionPromise.ApplicationCommand.ReplyScheme;

            switch (replyScheme)
            {
            case ApplicationCommandReplySchemes.None:
                _logger.LogWarning(FormatReplyMessage(notification, prefix, $"but the return schema is {replyScheme}"));
                break;

            case ApplicationCommandReplySchemes.OnDomainCommandHandled:

                var replySchemeReceived = notification.ApplicationCommandReplyScheme;

                if (replyScheme != replySchemeReceived)
                {
                    _logger.LogWarning(FormatReplyMessage(notification, prefix, $"but return schema mis match, expected: {replyScheme}, actual: {replySchemeReceived}"));
                }

                var status = notification.IsCompleted
                        ? ApplicationCommandStatus.Succeed
                        : ApplicationCommandStatus.Failed;
                var result = new ApplicationCommandResult <string>(applicationCommandId, applicationCommandType, status, notification.Message);

                if (!executionPromise.TrySetResult(result))
                {
                    _logger.LogError(FormatReplyMessage(notification, "Failed to set the saga domain notification of application Command", $"ReplyScheme:{replyScheme}, message:{notification.Message}"));
                }

                break;

            case ApplicationCommandReplySchemes.OnDomainEventHandled:
                _logger.LogWarning(FormatReplyMessage(notification, prefix, $"but the return schema:{replyScheme} mis-match"));
                break;

            default:
                _logger.LogWarning(FormatReplyMessage(notification, prefix, $"but the return schema:{replyScheme} not supported"));
                break;
            }
        }
        private void SetApplicationCommandResult(DomainExceptionMessage exception)
        {
            var applicationCommandId   = exception.ApplicationCommandId;
            var applicationCommandType = exception.ApplicationCommandType;

            const string prefix = "Received the execution notification of application Command";

            if (!ExecutingPromiseDict.TryRemove(applicationCommandId, out var executionPromise))
            {
                _logger.LogWarning(FormatReplyMessage(exception, prefix, "but task source does not exist"));

                return;
            }

            var replyScheme = executionPromise.ApplicationCommand.ReplyScheme;

            switch (replyScheme)
            {
            case ApplicationCommandReplySchemes.None:
                _logger.LogWarning(FormatReplyMessage(exception, prefix, $"but the return schema is {replyScheme}"));
                break;

            case ApplicationCommandReplySchemes.OnDomainCommandHandled:

                var replySchemeReceived = exception.ApplicationCommandReplyScheme;

                if (replyScheme != replySchemeReceived)
                {
                    _logger.LogWarning(FormatReplyMessage(exception, prefix, $"but return schema mis match, expected: {replyScheme}, actual: {replySchemeReceived}"));
                }

                var result = ApplicationCommandResult.Failed(applicationCommandId, applicationCommandType, exception.Message);

                if (!executionPromise.TrySetResult(result))
                {
                    _logger.LogError(FormatReplyMessage(exception, "Failed to set the domain exception of application Command", $"ReplyScheme:{replyScheme}, result:{result}"));
                }

                break;

            case ApplicationCommandReplySchemes.OnDomainEventHandled:
                _logger.LogWarning(FormatReplyMessage(exception, prefix, $"but the return schema:{replyScheme} mis-match"));
                break;

            default:
                _logger.LogWarning(FormatReplyMessage(exception, prefix, $"but the return schema:{replyScheme} not supported"));
                break;
            }
        }
        private void CancelApplicationCommand(ApplicationCommandExecutionPromise promise, int delay)
        {
            var command     = promise.ApplicationCommand;
            var commandId   = command.Id;
            var commandType = command.GetType().FullName;

            if (!ExecutingPromiseDict.TryRemove(commandId, out var executionPromise))
            {
                _logger.LogTrace($"Over execution timeout upper limit: {delay} seconds, Cancelling application Command, Id:{commandId}, Type:{commandType}, but task source does not exist.");

                return;
            }

            var replyScheme = command.ReplyScheme;
            var result      = ApplicationCommandResult.TimeOuted(commandId, commandType, "The execution of application command timeout, please try again.");

            _logger.LogWarning(executionPromise.TrySetResult(result)
                ? $"Over execution timeout upper limit: {delay} seconds, cancelled application Command, Id:{commandId}, Type:{commandType}, ReplyScheme:{replyScheme}."
                : $"Over execution timeout upper limit: {delay} seconds, Failed to cancel application Command, Id:{commandId}, Type:{commandType}, ReplyScheme:{replyScheme}.");
        }
 public bool TrySetResult(ApplicationCommandResult result) => _completionSource.TrySetResult(result);