I'm trying to implement HTTP server sent events as shown in this Stack Overflow answer. The answer fires off a notification to all subscribers on event. I'd like to have only a specific client receive notification for an event (so only the client who did something to trigger an event will be notified, instead of everyone).
I'm using EventHandlerList to register handlers, using a Guid as key. Relevant snippet of code:
public class MessageRepository
{
private readonly EventHandlerList _eventHandlerList;
public MessageRepository()
{
_eventHandlerList = new EventHandlerList();
}
public void Subscribe(Guid guid, Delegate handler)
{
_eventHandlerList.AddHandler(guid, handler);
}
public void Unsubscribe(Guid guid, Delegate handler)
{
_eventHandlerList.RemoveHandler(guid, handler);
}
public void Notify(Guid guid, string message)
{
_eventHandlerList[guid]?.DynamicInvoke(this, new Message(message));
}
}
Unfortunately, _eventHandlerList[guid]
always comes back as null. I've verified that a similar implementation where Dictionary<Guid, Delegate>
is used in place of EventHandlerList works correctly. Why?