I am using Ninject’s bbEventBroker extension to wire up some pub/sub in my application. I have business services which publish events, and then I have other classes which subscribe to events. The wiring with Ninject is working, with one problem. I don’t see an obvious place where I should instantiate the event subscribers. Right now I am hacking it is, and just making sure all of the subscribers are requested into the kernel as singletons before anyone else uses it. But this doesn’t seem right. If I do nothing, then there are no instances of the subscribers, and the events end up getting ignored.
How should I instantiate the subscribers to bbEventBroker events so that they end up wired into the kernel?
I ended up creating some pretty hacky code to solve this, but it does work. In my Ninject module which wires up the event subscribers, I also add an OnActivation handler for IEventBroker. When that activation handler is called, I do a Get on the Kernel for each subscriber (by using reflection to find the right types). This ensures that an instance of each subscriber is wired into the broker.
This leads to an issue where, for some reason, IEventBrokers can be activated while Ninject is trying to dispose. So, I had to override OnDispose on my module and set a flag to stop doing the above once dispose has started. Hacks upon hacks! 🙂