It has been a long time (10 months) since I worked with the Ninject MessageBroker and a couple things have changed since my last post. In the old version you had to connect the MessageBroker as a kernel component by hand. Since then the extension environment has been flushed out a lot more. Now you can register the MessageBrokerModule when constructing your kernel object.
usingSystem;usingSystem.Diagnostics;usingSystem.Net;usingSystem.Text.RegularExpressions;usingNinject;usingNinject.Extensions.MessageBroker;namespaceNinjectMessageBroker{internalclassProgram{privatestaticvoidMain(){// Intialize our injection kernel adding message broker functionality.using(varkernel=newStandardKernel(newMessageBrokerModule())){// Get the event publisher. It reads the current time and fires an eventvarpub=kernel.Get<TimeReader>();Debug.Assert(pub!=null);// Get the subscriber, it waits to get the current time and writes it to stdoutvarsub=kernel.Get<TimeWriter>();Debug.Assert(sub!=null);// Verify that they were wired togetherDebug.Assert(pub.HasListeners);Debug.Assert(sub.LastMessage==null);// Get the current time. It should automatically let the TimeWriter know// without either of them ever knowing of one another.pub.GetCurrentTime();// Wait to exit.Console.ReadLine();}}}internalclassTimeWriter{publicstringLastMessage{get;set;} [Subscribe("message://Time/MessageReceived")]publicvoidOnMessageReceived(objectsender,EventArgs<string>args){LastMessage=args.EventData;Console.WriteLine(LastMessage);}}internalclassTimeReader{publicboolHasListeners{get{return(MessageReceived!=null);}} [Publish("message://Time/MessageReceived")]publiceventEventHandler<EventArgs<string>>MessageReceived;/// <summary>/// Gets the current time and updates all subscribers./// </summary>publicvirtualvoidGetCurrentTime(){stringtext=GetWebPage();varregex=newRegex(@"dd:dd:dd");MatchCollectionmatches=regex.Matches(text);stringtime=((matches.Count==2)?matches[1]:matches[0]).Value;SendMessage(time);}/// <summary>/// Gets the contents of a web page as a string./// </summary>/// <returns></returns>privatestaticstringGetWebPage(){conststringurl="http://www.time.gov/timezone.cgi?Eastern/d/-5";varwebClient=newWebClient();returnwebClient.DownloadString(url);}/// <summary>/// Sends the message to all subscribers in a threadsafe manner./// </summary>/// <param name="message">The message.</param>publicvoidSendMessage(stringmessage){EventHandler<EventArgs<string>>messageReceived=MessageReceived;if(messageReceived!=null){messageReceived(this,newEventArgs<string>(message));}}}publicclassEventArgs<TData>:EventArgs{publicnewstaticreadonlyEventArgs<TData>Empty;staticEventArgs(){Empty=newEventArgs<TData>();}privateEventArgs(){}publicEventArgs(TDataeventData){EventData=eventData;}publicTDataEventData{get;privateset;}}}