I am trying to hook up interface.MessageReceived
to logger.LogReceivedMessage
. If I add
interface.MessageReceived += LogReceivedMessage;
in logger
's constructor, LogReceivedMessage
is called as expected.
However, if I move the hookup to some other place in logger
, LogReceivedMessage
is never called.
All other hookups to interface.MessageReceived
use +=
and not =
, so that's not the problem.
I used the Watch window and "Make Object ID" option in Visual Studio to verify that the code has the same instance of interface
both places. It does. However, interface.MessageReceived
has a different object in the constructor than the other places in the code. In fact, it seems to change every time something is hooked up to it. I'm not sure if that is expected or not.
Does anyone have any ideas why I can only hook up the handler in the constructor?
Edit:
I've gotten it working, but I'm not sure why it works. Original code in the interface
class:
public event Action<Message> MessageReceived;
busClient.MessageReceived += MessageReceived
I changed it to:
public event Action<Message> intermediateMessageReceived;
public event Action<Message> MessageReceived;
busClient.MessageReceived += intermediateMessageReceived;
public void intermediateMessageReceived(Message m)
{
MessageReceived(m);
}
Without posting all the code to the entire project, does anyone have any idea why this behaves differently?