protected async override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                foreach (var eventLog in _eventSourcingDbContext.EventLogs.Where(_ => _.Status == 0).ToList())
                {
                    var assembly = assemblies.FirstOrDefault(_ => _.GetName().Name == eventLog.AssemblyName);
                    if (assembly == null)
                    {
                        assembly = Assembly.Load(eventLog.AssemblyName);
                        assemblies.Add(assembly);
                    }
                    var type = types.FirstOrDefault(_ => _.FullName == eventLog.TypeName);
                    if (type == null)
                    {
                        type = assembly.GetType(eventLog.TypeName);
                        types.Add(type);
                    }

                    var json = Encoding.UTF8.GetString(eventLog.Content);
                    var obj  = JsonConvert.DeserializeObject(json, type);

                    _eventBus.Publish(type, (EventBusMessage)obj);

                    _eventSourcingDbContext.Remove(eventLog);

                    await _eventSourcingDbContext.SaveChangesAsync(stoppingToken);
                }
                await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
            }
        }
Esempio n. 2
0
        public async Task SaveEventLogAsync(EventBusMessage message, IDbContextTransaction transaction, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (_eventSourcingDbContext.Database.CurrentTransaction == null ||
                _eventSourcingDbContext.Database.CurrentTransaction.GetDbTransaction() != transaction.GetDbTransaction())
            {
                _eventSourcingDbContext.Database.UseTransaction(transaction.GetDbTransaction());
            }

            _eventSourcingDbContext.EventLogs.Add(new EventLog(message));

            await _eventSourcingDbContext.SaveChangesAsync(cancellationToken);
        }
Esempio n. 3
0
 public Task Handle(SaveChangesWhenAddedEventLogEvent notification, CancellationToken cancellationToken)
 {
     return(_eventSourcingDbContext.SaveChangesAsync(cancellationToken));
 }