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.
Async await, code never getting executed [closed]
-
13-07-2023 - |
Pregunta
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());
}
});
}
Solución
Otros consejos
Changed return new Task to return Task.Run, that solved it
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow