private async Task PublishEventsAsync() { if (publishedEvents.Count > 0) { if (EventBuffer == null) { throw new InvalidOperationException($"Cannot publish events from {this} because its {nameof(EventBuffer)} has not been set"); } if (EventMessageFactory == null) { throw new InvalidOperationException($"Cannot publish events from {this} because its {nameof(EventMessageFactory)} has not been set"); } } foreach (var ev in publishedEvents) { var eventMessage = await EventMessageFactory.CreateMessageAsync(ev); if (eventMessage.Metadata.GetEventId() == null) { eventMessage.SetMetadata(BasicEventMetadataNames.EventId, Guid.NewGuid().ToString()); } EventBuffer.PushEvent(eventMessage); } }