C#C
C#2y ago
M B V R K

Why Events Inserted Twice by EF Core?

Hi guys hope you're doing well,
I'm working on the following project : https://github.com/MbarkT3STO/ExpenovaApp
This project implemented using Microservices Architecture
I have a service called Event-Sourcerer Service, this service is responsible about applying event-sourcing for others services databases records, of course this services communicate with others using RabbitMQ and MassTransit.
You can take a look on that service here : https://github.com/MbarkT3STO/ExpenovaApp/tree/main/Source/EventSourcererService

This service contains some Message Consumers ( classes where each one responsible to consume/handle a specific Queue's messages ).
This service uses EF Core with PostgreSQL as data persistence.
After a Servie X published an Event that happened in it, and the Event Handler from that Service X will publish a copy of that event as a Message to RabbitMQ then the Consumers from the Event-Sourcerer Service will do their job by adding the message they are responsible for into the Event-Sources database.


The issue: Is my consumers adding the event to database twice
To fix this issue I have created a service to handle the Deduplication mechanism as separated class called DatabaseMessageDeduplicationService you could take a look at it here : https://github.com/MbarkT3STO/ExpenovaApp/blob/main/Source/EventSourcererService/Services/DatabaseMessageDeduplicationService.cs

That class DatabaseMessageDeduplicationService is responsible about processing messages too.
So, for proccessing messages this class contains a method called ProccessMessage as following:
    public async Task ProcessMessage(Expression<Func<Task>> processMessageFunc)
    {
        await processMessageFunc.Compile().Invoke();
    }

This method take a func/method from a consumer, that method consumer will contains the code to process the message and this DatabaseMessageDeduplicationService only invoking it.
Was this page helpful?