Why event is not triggered correctly?
I have a unit of work pattern and when I save an item I want to fire an event to trigger a sync method in another service class.
Unfortunatelly this way it is not triggered
what am I doing wrong?
7 Replies
that's scoped, you need a persisten "bus" to send events
from the looks of it
although i guess if set right it should work
So somthing like WeakReferenceMessenger would be better fit for this?
i never used that, sorry
Hey, just my thought: You register the RemoteSyncHandler but where is it getting constructed/injected somewhere?
I mean if you set a breakpoint in the lambda in the IoC does this every getting triggered?
services.AddScoped<IUnitOfWork>(provider =>
{
var uow = new UnitOfWork();
var remoteSyncHandler = provider.GetRequiredService<RemoteSyncHandler>();
uow.EntitySaved += remoteSyncHandler.OnChangesSaved;
return uow;
});
and just register RemoteSyncHandler and the SyncAgent as Scoped too.
i would study the problem for a while to be certain this (captring SaveChanges) is the correct solution to apply
because depending on the urgency of the sync and on the size of the data a timed batch sync could also work
@SevenMgCreatine You are right it is not hit the breakpoint at all. I did not instantiated at all.
By ensuring proper event subscription, managing instances correctly, and handling asynchronous calls appropriately, your synchronization process should trigger as expected when an entity is saved in your unit of work