/// <summary> /// Register an event handler that will listen and respond to all events. /// </summary> public void RegisterGlobalEventHandler <TMessage>(Action <TMessage> handler, bool holdMessageLock = true) where TMessage : IMessage { Action <TMessage> registerableHandler = BusHelper.BuildTelemeteredActionHandler <TMessage, TAuthenticationToken>(TelemetryHelper, handler, holdMessageLock, "In-Process/Bus"); Routes.RegisterGlobalEventHandler(registerableHandler, holdMessageLock); TelemetryHelper.TrackEvent(string.Format("Cqrs/RegisterGlobalEventHandler/{0}", typeof(TMessage).FullName), new Dictionary <string, string> { { "Type", "In-Process/Bus" } }); TelemetryHelper.Flush(); }
public virtual bool?ReceiveCommand(ICommand <TAuthenticationToken> command) { CorrelationIdHelper.SetCorrelationId(command.CorrelationId); AuthenticationTokenHelper.SetAuthenticationToken(command.AuthenticationToken); Type commandType = command.GetType(); bool response = true; bool isRequired = BusHelper.IsEventRequired(commandType); IList <Action <IMessage> > handlers; if (Routes.TryGetValue(commandType, out handlers)) { if (handlers != null) { if (handlers.Count != 1) { throw new MultipleCommandHandlersRegisteredException(commandType); } if (handlers.Count == 1) { handlers.Single()(command); } else if (isRequired) { throw new NoCommandHandlerRegisteredException(commandType); } else { response = false; } } else if (isRequired) { throw new NoCommandHandlerRegisteredException(commandType); } else { response = false; } } else if (isRequired) { throw new NoCommandHandlerRegisteredException(commandType); } else { response = false; } return(response); }
protected virtual bool PrepareAndValidateCommand <TCommand>(TCommand command, out RouteHandlerDelegate commandHandler) where TCommand : ICommand <TAuthenticationToken> { Type commandType = command.GetType(); switch (command.Framework) { case FrameworkType.Akka: Logger.LogInfo(string.Format("A command arrived of the type '{0}' but was marked as coming from the '{1}' framework, so it was dropped.", commandType.FullName, command.Framework)); commandHandler = null; return(false); } ICommandValidator <TAuthenticationToken, TCommand> commandValidator = null; try { commandValidator = DependencyResolver.Resolve <ICommandValidator <TAuthenticationToken, TCommand> >(); } catch (Exception exception) { Logger.LogDebug("Locating an ICommandValidator failed.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName), exception); } if (commandValidator != null && !commandValidator.IsCommandValid(command)) { Logger.LogInfo("The provided command is not valid.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName)); commandHandler = null; return(false); } PrepareCommand(command); bool isRequired = BusHelper.IsEventRequired(commandType); commandHandler = Routes.GetSingleHandler(command, isRequired); // This check doesn't require an isRequired check as there will be an exception raised above and handled below. if (commandHandler == null) { Logger.LogDebug(string.Format("The command handler for '{0}' is not required.", commandType.FullName)); return(false); } return(true); }
protected virtual bool PrepareAndValidateCommand <TCommand>(TCommand command, out RouteHandlerDelegate commandHandler) where TCommand : ICommand <TAuthenticationToken> { Type commandType = command.GetType(); if (command.Frameworks != null && command.Frameworks.Contains("Built-In")) { Logger.LogInfo("The provided command has already been processed by the Built-In bus.", string.Format("{0}\\PrepareAndValidateEvent({1})", GetType().FullName, commandType.FullName)); commandHandler = null; return(false); } ICommandValidator <TAuthenticationToken, TCommand> commandValidator = null; try { commandValidator = DependencyResolver.Resolve <ICommandValidator <TAuthenticationToken, TCommand> >(); } catch (Exception exception) { Logger.LogDebug("Locating an ICommandValidator failed.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName), exception); } if (commandValidator != null && !commandValidator.IsCommandValid(command)) { Logger.LogInfo("The provided command is not valid.", string.Format("{0}\\Handle({1})", GetType().FullName, commandType.FullName)); commandHandler = null; return(false); } PrepareCommand(command); bool isRequired = BusHelper.IsEventRequired(commandType); commandHandler = Routes.GetSingleHandler(command, isRequired); // This check doesn't require an isRequired check as there will be an exception raised above and handled below. if (commandHandler == null) { Logger.LogDebug(string.Format("The command handler for '{0}' is not required.", commandType.FullName)); return(false); } return(true); }