Pergunta

Im having this method to commit changes to an eventstore, and then publishing the events after the eventstore has gotten updated.
My problem is that the code never reach the aggregate.MarkChangesAsCommitted method and the next await.

    public async Task CommitChanges()
    {
        foreach (var aggregate in _trackedAggregates.Values)
        {
            var newEvents = aggregate.GetChanges();
            await _eventStorage.Save(aggregate.Id, newEvents);
            aggregate.MarkChangesAsCommitted();
            await _eventPublisher.Publish(newEvents);
        }
    }

Event store

    public Task Save(Guid aId, IEnumerable<IDomainEvent> events)
    {
        return new Task(() =>
        {
            using (var stream = _store.OpenStream(aId))
            {
                foreach (var domainEvent in events)
                {
                    stream.Add(new EventMessage
                    {
                        Body = domainEvent
                    });
                }
                stream.CommitChanges(Guid.NewGuid());
            }
        });
    }
Foi útil?

Solução

new Task() doesn't start the task, you need to then call task.Start() to start it. Task.Run returns an already started task, also called a hot task.

Outras dicas

Changed return new Task to return Task.Run, that solved it

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top