I'm currently implementing a SignalR application to send a message to several clients when an external library I've written fire an event.
I want to attach a unique handler to my event for every instance of my hub, so I could send the messages only once when it is fired; but as SignalR instanciate many hubs on every request, I end up with more than one event handler attached every time. So my question is: what can I do to attach only once on my event ?
Here is my code:
public class MyHub : Hub
{
private static ExternalClass staticObject = new ExternalClass();
public MyHub()
{
staticObject.MyEvent += staticObject_MyEvent;
}
private void staticObject_MyEvent(object sender, EventArgs e)
{
//Some irrelevant code which send messages to clients
}
}
I know my question is pretty the same than this one and a lot of others, but I never found a satisfying anwser for multi-threaded or multi-instance environment - which is definitly my case.