質問

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());
            }
        });
    }
役に立ちましたか?

解決

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.

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top