Beispiel #1
0
        public async Task SaveAsync <T>(T saga) where T : ISaga
        {
            var sagaType = saga.GetType().FullName;
            var entity   = await db.Sagas.FindAsync(saga.Id, sagaType);

            if (entity == null)
            {
                entity = new SagaEntity
                {
                    Id            = saga.Id,
                    CorrelationId = saga.CorrelationId,
                    Type          = sagaType,
                    Completed     = saga.Completed,
                    Payload       = sagaSerializer.Serialize(saga)
                };

                db.Sagas.Add(entity);
            }
            else
            {
                entity.Payload = sagaSerializer.Serialize(saga);
            }

            db.SaveChanges();

            await PutCommandsToBus(entity, saga);
        }
        public IHttpActionResult PutInfrastructure(int id, Infrastructure infrastructure)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != infrastructure.Infrastructure_id)
            {
                return(BadRequest());
            }

            db.Entry(infrastructure).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!InfrastructureExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #3
0
        public async Task PublishEventsAsync(params IntegrationEvent[] events)
        {
            if (events != null)
            {
                foreach (var @event in events)
                {
                    var entity = new IntegrationEventEntity
                    {
                        Id            = @event.MessageId,
                        CorrelationId = httpContextAccessor.HttpContext?.TraceIdentifier,
                        ExecutedBy    = httpContextAccessor.HttpContext?.User.GetId(),
                        Type          = @event.GetType().AssemblyQualifiedName,
                        FullName      = @event.GetType().FullName,
                        Payload       = messageSerializer.Serialize(@event),
                        Date          = DateTimeOffset.UtcNow
                    };

                    db.IntegrationEvents.Add(entity);

                    db.SaveChanges();
                    logger.Info("[ServiceBus->PublishEventsAsync] Sending integration event of type: {0}", @event.GetType().Name);

                    var brokeredMessage = new Microsoft.Azure.ServiceBus.Message(entity.Payload);

                    brokeredMessage.UserProperties["CorrelationId"] = entity.CorrelationId;
                    brokeredMessage.UserProperties["Name"]          = @event.GetType().Name;
                    brokeredMessage.UserProperties["FullName"]      = @event.GetType().FullName;
                    brokeredMessage.UserProperties["Namespace"]     = @event.GetType().Namespace;
                    brokeredMessage.UserProperties["Type"]          = @event.GetType().AssemblyQualifiedName;
                    brokeredMessage.UserProperties["EventType"]     = "Integration";

                    await topicClient.SendAsync(brokeredMessage);
                }
            }
        }
Beispiel #4
0
        public async Task SendCommandAsync <T>(T command) where T : Command
        {
            logger.Info("[CommandBus->SendCommandAsync] Sending command of type: {0}", typeof(T).Name);

            var entity = new CommandEntity
            {
                Id            = command.MessageId,
                CorrelationId = httpContextAccessor.HttpContext?.TraceIdentifier,
                ExecutedBy    = httpContextAccessor.HttpContext?.User.GetId(),
                Type          = command.GetType().AssemblyQualifiedName,
                FullName      = command.GetType().FullName,
                ScheduledAt   = DateTimeOffset.UtcNow,
                Payload       = messageSerializer.Serialize(command)
            };

            db.Commands.Add(entity);

            db.SaveChanges();

            try
            {
                var handlerType = typeof(IHandler <>).MakeGenericType(command.GetType());
                var handler     = serviceProvider.GetService(handlerType);

                if (handler != null)
                {
                    await(Task) handler.AsDynamic().HandleAsync(command);
                }
            }
            catch (Exception e)
            {
                entity.ErrorDescription = e.ToString();

                throw e;
            }
            finally
            {
                entity.Executed   = true;
                entity.ExecutedAt = DateTimeOffset.UtcNow;
                db.SaveChanges();
            }
        }
        public void RequestTimeout <T>(T approvalProcessTimeout, TimeSpan afterTime)
        {
            _context.RequestedTimeouts.Add(new RequestedTimeout
            {
                Id              = Guid.NewGuid(),
                TimeoutTime     = DomainTime.Current.Now.Add(afterTime),
                TimeoutDataJson = JsonConvert.SerializeObject(approvalProcessTimeout),
                TimeoutType     = typeof(T).AssemblyQualifiedName
            });

            _context.SaveChanges();
        }
Beispiel #6
0
        public Task SaveAsync <T>(T item) where T : AggregateRoot
        {
            var entity = new SnapshotEntity
            {
                AggregateId = item.Id,
                SourceType  = item.GetType().FullName,
                Version     = item.Version,
                Payload     = aggregateSerializer.Serialize(item)
            };

            db.Snapshots.Add(entity);

            db.SaveChanges();

            return(Task.FromResult(true));
        }
Beispiel #7
0
        private DbContextOptions <InfrastructureContext> PopulateContext()
        {
            var options = SetupContext();

            using (var context = new InfrastructureContext(options))
            {
                context.Sagas.Add(GenerateSagaEntity(ToGuid(1), false, false));
                context.Sagas.Add(GenerateSagaEntity(ToGuid(2), false, true));
                context.Sagas.Add(GenerateSagaEntity(ToGuid(3), true, false));
                context.Sagas.Add(GenerateSagaEntity(ToGuid(4), true, true));

                context.SaveChanges();
            }

            return(options);
        }
        private DbContextOptions <InfrastructureContext> PopulateContext()
        {
            var options = SetupContext();

            using (var context = new InfrastructureContext(options))
            {
                context.Snapshots.Add(GenerateSnapshotEntity(ToGuid(1), 10));
                context.Snapshots.Add(GenerateSnapshotEntity(ToGuid(1), 20));
                context.Snapshots.Add(GenerateSnapshotEntity(ToGuid(2), 10));
                context.Snapshots.Add(GenerateSnapshotEntity(ToGuid(2), 20));
                context.Snapshots.Add(GenerateSnapshotEntity(ToGuid(2), 30));

                context.SaveChanges();
            }

            return(options);
        }
Beispiel #9
0
        private DbContextOptions <InfrastructureContext> PopulateContext()
        {
            var options = SetupContext();

            using (var context = new InfrastructureContext(options))
            {
                context.Events.Add(GenerateEventEntity(Aggregate1, 1));
                context.Events.Add(GenerateEventEntity(Aggregate1, 2));
                context.Events.Add(GenerateEventEntity(Aggregate1, 3));
                context.Events.Add(GenerateEventEntity(Aggregate1, 4));

                context.Events.Add(GenerateEventEntity(Aggregate2, 1));
                context.Events.Add(GenerateEventEntity(Aggregate2, 2));
                context.Events.Add(GenerateEventEntity(Aggregate2, 3));
                context.Events.Add(GenerateEventEntity(Aggregate2, 4));

                context.SaveChanges();
            }

            return(options);
        }
Beispiel #10
0
        public async Task SendCommandAsync <T>(T command) where T : Command
        {
            logger.Info("[ServiceBus->SendCommandAsync] Sending command of type: {0}", typeof(T).Name);

            var entity = new CommandEntity
            {
                Id            = command.CommandId,
                CorrelationId = command.CorrelationId,
                ExecutedBy    = command.ExecutedBy,
                Type          = command.GetType().FullName,
                ScheduledAt   = DateTimeOffset.UtcNow,
                Payload       = messageSerializer.Serialize(command)
            };

            db.Commands.Add(entity);

            await db.SaveChangesAsync();

            try
            {
                await SendMessage(command, commandsQueueClient);

                entity.Success = true;
            }
            catch (Exception e)
            {
                entity.ErrorDescription = e.ToString();

                throw e;
            }
            finally
            {
                entity.Executed   = true;
                entity.ExecutedAt = DateTimeOffset.UtcNow;
                db.SaveChanges();
            }
        }
Beispiel #11
0
        public Task SaveChangesAsync()
        {
            db.SaveChanges();

            return(Task.FromResult(true));
        }
 public void Execute(IJobExecutionContext context)
 {
     _innerJob.Execute(context);
     _context.SaveChanges();
 }