public void SendToHandler(string id, IEnvelope envelope) { IDispatchMessage dispatcher; if (Dispatchers.TryGetValue(envelope.MessageType, out dispatcher)) { dispatcher.Dispatch(envelope); } else { IEnumerable <Type> inheritanceChain = Reflector.GetInheritanceChain(envelope.MessageType); Type key = null; if ((key = Dispatchers.Keys.FirstOrDefault(k => inheritanceChain.Contains(k))) != null) { dispatcher = Dispatchers[key]; Dispatchers.AddOrUpdate(envelope.MessageType, x => dispatcher, (x, y) => dispatcher); dispatcher.Dispatch(envelope); } // message is a response to a request message else if (ResponseDispatchers.TryRemove(envelope.CorrelationId, out dispatcher)) { dispatcher.Dispatch(envelope); } } }
public void WireupDispatchers() { if (Dispatchers.Count == 0) { var dispatchers = Assimilate.GetAllInstancesOf <IDispatchMessage>(); dispatchers .ForEach(x => x.Handles.ForEach(y => Dispatchers.AddOrUpdate(y, x, (t, m) => x))); } // prevent duplicate instantiations var agency = Assimilate.GetInstanceOf <IAgency>(); agency.RegisterActorOf("", this); // prime director //Fibers.Send("", new Envelope<PrimeDirector>(new PrimeDirector()) { CorrelationId = "" }); Loop.Enqueue(() => SendToHandler("", new Envelope <PrimeDirector>(new PrimeDirector()) { CorrelationId = "" })); Signal.Wait(100); }