Skip to content

Duplicated metadata key when implementing IMetadataProvider #1136

@foliv57

Description

@foliv57

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions