-
-
Notifications
You must be signed in to change notification settings - Fork 469
Description
Hi,
I'm trying to play around with IMetadataProvider. My ultimate goal is to create a provider that injects a "saga-id" to the metadata by extracting it from aggregate event (if this event implements a specific ISagaEvent).
It's not a super design, but it was more to test the IMetadataProvider and to have a workaround for my ISagaLocator.
My problem is that my IMetadataProvider is triggered multiple time for the same event and ends by throwing a "An item with the same key has already been added. Key: saga-id" error.
The implementation of my IMetadataProvider is:
public class SagaIdMetadataProvider : IMetadataProvider
{
public IEnumerable<KeyValuePair<string, string>> ProvideMetadata<TAggregate, TIdentity>(TIdentity id, IAggregateEvent aggregateEvent, IMetadata metadata)
where TAggregate : IAggregateRoot<TIdentity>
where TIdentity : IIdentity
{
if (!metadata.ContainsKey("saga-id") && aggregateEvent is ISagaEvent sagaEvent)
{
yield return new KeyValuePair<string, string>("saga-id", sagaEvent.GetSagaId().Value);
}
}
}And I register it like this:
builder.Services.AddEventFlow(ef =>
{
ef.AddDefaults(typeof(Application).Assembly);
ef.AddMetadataProvider<SagaIdMetadataProvider>();
});My event is only instantiated and emitted ones like this:
public IExecutionResult PlaceOrder(int numberOfPersons)
{
Emit(new OrderPlaced(this.Id, numberOfPersons));
return ExecutionResult.Success();
}This event is the event that starts the Saga with the implementation of ISagaIsStartedBy<Order, OrderId, OrderPlaced>.
Thanks for your help.
Dotnet version: 8.0
EventFlow version: 1.2.2