Using a class with event

Tags: #<Tag:0x00007f417da3f7f8>


I am attempting to use Hangfire in a Windows Service.
Here’s my problem. I am trying to call Run method of a simple EmailMonitor class.
Trivially, when the class discover a new email, it raise one event, OnEmailReceived.

Inside code we have: a class for custom EventArgs ( it’s event with specific data), the delegate for the event, the event, and the protect method that raises the event…ie

protected virtual void OnEmailReceived (EmailReceivedEventArgs e)
      if (EmailReceived != null) EmailReceived(this, e);

When I call the method directly, EmailMonitor.Run(EleID) all works well, but with HangFire, ie:

RecurringJob.AddOrUpdate(EleID, () => EmailMonitor.Run(EleID), Cron.MinuteInterval(5));

when the job runs, the event EmailReceived is always null (!), so that no “event” is raised.

Question: it’s possible to serialize a class with an event? Is the meaning wrong?

My suspicion is that there are no event handlers attached to this event when HangFire executes the job, so my workaround is “not use” events and call directly the “management method” associated with the event.
Any suggestion? Thanks in advance