/// <inheritdoc/>
        public Task <QMessage <T> > Dequeue()
        {
            var sql = @"
                SET NOCOUNT ON; 
                WITH cte AS (
                    SELECT TOP(1) * 
                    FROM [work].[QMessage] WITH (ROWLOCK, READPAST)
                    WHERE [QueueName] = @QueueName
                    ORDER BY [Date] ASC
                )
                DELETE FROM cte 
                OUTPUT [deleted].*;
            ";

            using (var dbConnection = _dbConnectionFactory.Create()) {
                dbConnection.Open();
                var command = dbConnection.CreateCommand();
                command.AddParameterWithValue("@QueueName", _queueNameResolver.Resolve(), DbType.String);
                command.CommandText = sql;
                command.CommandType = CommandType.Text;
                var        dataReader = command.ExecuteReader();
                DbQMessage message    = null;
                while (dataReader.Read())
                {
                    message = new DbQMessage {
                        Id                                                                   = dataReader.GetGuid(0),
                        QueueName                                                            = dataReader.IsDBNull(1) ? default : dataReader.GetString(1),
                                                           Payload                           = dataReader.IsDBNull(2) ? default : dataReader.GetString(2),
                                                                                  Date       = dataReader.GetDateTime(3),
                                                                                  RowVersion = dataReader.IsDBNull(4) ? default : (byte[])dataReader[nameof(DbQMessage.RowVersion)],
                                                                                               DequeueCount = dataReader.GetInt32(5),
                                                                                               State        = (QMessageState)dataReader.GetInt32(6)
                    };
                }
                return(Task.FromResult(message != null ? message.ToModel <T>(_jsonSerializerOptions) : default));
Exemplo n.º 2
0
        /// <inheritdoc/>
        public async Task <QMessage <T> > Dequeue()
        {
            var dbConnection = await _dbContext.Database.EnsureOpenConnectionAsync();

            using (var command = dbConnection.CreateCommand()) {
                command.AddParameterWithValue("@QueueName", _queueNameResolver.Resolve(), DbType.String);
                command.CommandText = _queryDescriptor.Dequeue;
                command.CommandType = CommandType.Text;
                using (var dataReader = await command.ExecuteReaderAsync()) {
                    DbQMessage message = null;
                    while (dataReader.Read())
                    {
                        message = new DbQMessage {
                            Id                                                                   = dataReader.GetGuid(0),
                            QueueName                                                            = dataReader.IsDBNull(1) ? default : dataReader.GetString(1),
                                                               Payload                           = dataReader.IsDBNull(2) ? default : dataReader.GetString(2),
                                                                                      Date       = dataReader.GetDateTime(3),
                                                                                      RowVersion = dataReader.IsDBNull(4) ? default : dataReader.GetValue(4) as byte[],
                                                                                                   DequeueCount = dataReader.GetInt32(5),
                                                                                                   State        = (QMessageState)dataReader.GetInt32(6)
                        };
                    }
                    return(message != null?message.ToModel <T>(_jsonSerializerOptions): default);