Exemple #1
0
        /// <summary>
        ///     Awaitably adds the specified identifier.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="command">The command.</param>
        /// <param name="contextKey">An identifier for the context in which the command has been processed (for example, the name of the handler)</param>
        /// <param name="timeoutInMilliseconds">Timeout in milliseconds; -1 for default timeout</param>
        /// <param name="cancellationToken">Allow the sender to cancel the request, optional</param>
        /// <returns><see cref="Task" />.</returns>
        public async Task AddAsync <T>(T command, string contextKey, int timeoutInMilliseconds = -1, CancellationToken cancellationToken = default(CancellationToken))
            where T : class, IRequest
        {
            var parameters = InitAddDbParameters(command, contextKey);

            using (var connection = await _connectionProvider.GetConnectionAsync(cancellationToken))
            {
                await connection.OpenAsync(cancellationToken).ConfigureAwait(ContinueOnCapturedContext);

                var sqlcmd = InitAddDbCommand(connection, parameters, timeoutInMilliseconds);
                try
                {
                    await sqlcmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(ContinueOnCapturedContext);
                }
                catch (SqlException sqlException)
                {
                    if (sqlException.Number == MsSqlDuplicateKeyError_UniqueIndexViolation || sqlException.Number == MsSqlDuplicateKeyError_UniqueConstraintViolation)
                    {
                        s_logger.LogWarning(
                            "MsSqlOutbox: A duplicate Command with the CommandId {Id} was inserted into the Outbox, ignoring and continuing",
                            command.Id);
                        return;
                    }

                    throw;
                }
            }
        }
Exemple #2
0
        /// <summary>
        ///     Gets the specified message identifier.
        /// </summary>
        /// <param name="messageId">The message identifier.</param>
        /// <param name="outBoxTimeout"></param>
        /// <returns>Task&lt;Message&gt;.</returns>
        public async Task AddAsync(Message message, int outBoxTimeout = -1, CancellationToken cancellationToken = default(CancellationToken))
        {
            var parameters = InitAddDbParameters(message);

            var connection = await _connectionProvider.GetConnectionAsync(cancellationToken);

            if (connection.State != ConnectionState.Open)
            {
                await connection.OpenAsync(cancellationToken).ConfigureAwait(ContinueOnCapturedContext);
            }
            using (var command = InitAddDbCommand(connection, parameters))
            {
                try
                {
                    if (_connectionProvider.IsSharedConnection)
                    {
                        command.Transaction = _connectionProvider.GetTransaction();
                    }
                    await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(ContinueOnCapturedContext);
                }
                catch (SqlException sqlException)
                {
                    if (sqlException.Number == MsSqlDuplicateKeyError_UniqueIndexViolation ||
                        sqlException.Number == MsSqlDuplicateKeyError_UniqueConstraintViolation)
                    {
                        s_logger.LogWarning(
                            "MsSqlOutbox: A duplicate Message with the MessageId {Id} was inserted into the Outbox, ignoring and continuing",
                            message.Id);
                        return;
                    }

                    throw;
                }
                finally
                {
                    if (!_connectionProvider.IsSharedConnection)
                    {
                        connection.Dispose();
                    }
                    else if (!_connectionProvider.HasOpenTransaction)
                    {
                        connection.Close();
                    }
                }
            }
        }
        /// <summary>
        ///     Add the passed message to the Queue (Async)
        /// </summary>
        /// <param name="message">The Message</param>
        /// <param name="topic">The topic name</param>
        /// <param name="timeoutInMilliseconds">Timeout in milliseconds; -1 for default timeout</param>
        /// <param name="cancellationToken">The active CancellationToken</param>
        /// <returns></returns>
        public async Task SendAsync(T message, string topic, int timeoutInMilliseconds = -1,
                                    CancellationToken cancellationToken = default(CancellationToken))
        {
            if (s_logger.IsEnabled(LogLevel.Debug))
            {
                s_logger.LogDebug("SendAsync<{CommandType}>(..., {Topic})", typeof(T).FullName, topic);
            }

            var parameters = InitAddDbParameters(topic, message);

            using (var connection = await _connectionProvider.GetConnectionAsync())
            {
                await connection.OpenAsync(cancellationToken).ConfigureAwait(ContinueOnCapturedContext);

                var sqlCmd = InitAddDbCommand(timeoutInMilliseconds, connection, parameters);
                await sqlCmd.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(ContinueOnCapturedContext);
            }
        }
Exemple #4
0
        /// <summary>
        /// Returns messages specified by the Ids
        /// </summary>
        /// <param name="outBoxTimeout">The Timeout of the outbox.</param>
        /// <param name="cancellationToken">Cancellation Token.</param>
        /// <param name="messageIds">The Ids of the messages</param>
        /// <returns></returns>
        public async Task <IEnumerable <Message> > GetAsync(IEnumerable <Guid> messageIds, int outBoxTimeout = -1,
                                                            CancellationToken cancellationToken = default(CancellationToken))
        {
            var connection = await _connectionProvider.GetConnectionAsync(cancellationToken);

            using (var command = connection.CreateCommand())
            {
                CreateListOfMessagesCommand(command, messageIds.ToList());

                if (connection.State != ConnectionState.Open)
                {
                    await connection.OpenAsync(cancellationToken).ConfigureAwait(ContinueOnCapturedContext);
                }

                var dbDataReader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(ContinueOnCapturedContext);

                var messages = new List <Message>();
                while (await dbDataReader.ReadAsync(cancellationToken))
                {
                    messages.Add(MapAMessage(dbDataReader));
                }
                dbDataReader.Close();

                if (!_connectionProvider.IsSharedConnection)
                {
                    connection.Dispose();
                }
                else if (!_connectionProvider.HasOpenTransaction)
                {
                    connection.Close();
                }

                return(messages);
            }
        }