Пример #1
0
        public async Task <Unit> Handle(ProcessOutboxCommand command, CancellationToken cancellationToken)
        {
            IDbConnection connection = _sqlConnectionFactory.GetOpenConnection();
            const string  sql        = "SELECT " +
                                       "[OutboxMessage].[Id], " +
                                       "[OutboxMessage].[Type], " +
                                       "[OutboxMessage].[Data] " +
                                       "FROM [app].[OutboxMessages] AS [OutboxMessage] " +
                                       "WHERE [OutboxMessage].[ProcessedDate] IS NULL";

            IEnumerable <OutboxMessageDto> messages = await connection.QueryAsync <OutboxMessageDto>(sql);

            List <OutboxMessageDto> messagesList = messages.AsList();

            const string sqlUpdateProcessedDate = "UPDATE [app].[OutboxMessages] " +
                                                  "SET [ProcessedDate] = @Date " +
                                                  "WHERE [Id] = @Id";

            if (messagesList.Count > 0)
            {
                foreach (OutboxMessageDto message in messagesList)
                {
                    Type?type = Assemblies.Application
                                .GetType(message.Type);
                    IDomainEventNotification request =
                        JsonConvert.DeserializeObject(message.Data, type) as IDomainEventNotification;

                    using (LogContext.Push(new OutboxMessageContextEnricher(request)))
                    {
                        await _mediator.Publish(request, cancellationToken);

                        await connection.ExecuteAsync(sqlUpdateProcessedDate, new { Date = DateTime.UtcNow, message.Id });
                    }
                }
            }

            return(Unit.Value);
        }
 public OutboxMessageContextEnricher(IDomainEventNotification notification)
 {
     _notification = notification;
 }