Esempio n. 1
0
        public virtual void DefaultReceiveCommand(ICommand <TAuthenticationToken> command, RouteManager routeManager, string framework)
        {
            Type commandType = command.GetType();

            if (command.Frameworks != null && command.Frameworks.Contains(framework))
            {
                Logger.LogInfo("The provided command has already been processed in Azure.", string.Format("{0}\\DefaultReceiveCommand({1})", GetType().FullName, commandType.FullName));
                return;
            }

            CorrelationIdHelper.SetCorrelationId(command.CorrelationId);
            AuthenticationTokenHelper.SetAuthenticationToken(command.AuthenticationToken);

            bool isRequired = BusHelper.IsEventRequired(commandType);

            RouteHandlerDelegate commandHandler = routeManager.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;
            }

            Action <IMessage> handler = commandHandler.Delegate;

            handler(command);
        }
Esempio n. 2
0
        /// <summary>
        /// The default command handler that
        /// check if the <see cref="ICommand{TAuthenticationToken}"/> has already been processed by this framework,
        /// checks if the <see cref="ICommand{TAuthenticationToken}"/> is required,
        /// finds the handler from the provided <paramref name="routeManager"/>.
        /// </summary>
        /// <param name="command">The <see cref="ICommand{TAuthenticationToken}"/> to process.</param>
        /// <param name="routeManager">The <see cref="RouteManager"/> to get the <see cref="ICommandHandler{TAuthenticationToken,TCommand}"/> from.</param>
        /// <param name="framework">The current framework.</param>
        /// <returns>
        /// True indicates the <paramref name="command"/> was successfully handled by a handler.
        /// False indicates the <paramref name="command"/> wasn't handled, but didn't throw an error, so by convention, that means it was skipped.
        /// Null indicates the command<paramref name="command"/> wasn't handled as it was already handled.
        /// </returns>
        public virtual bool?DefaultReceiveCommand(ICommand <TAuthenticationToken> command, RouteManager routeManager, string framework)
        {
            Type commandType = command.GetType();

            if (command.Frameworks != null && command.Frameworks.Contains(framework))
            {
                // if this is the only framework in the list, then it's fine to handle as it's just pre-stamped, if there is more than one framework, then exit.
                if (command.Frameworks.Count() != 1)
                {
                    Logger.LogInfo("The provided command has already been processed in Azure.", string.Format("{0}\\DefaultReceiveCommand({1})", GetType().FullName, commandType.FullName));
                    return(null);
                }
            }

            CorrelationIdHelper.SetCorrelationId(command.CorrelationId);
            AuthenticationTokenHelper.SetAuthenticationToken(command.AuthenticationToken);

            bool isRequired = BusHelper.IsEventRequired(commandType);

            RouteHandlerDelegate commandHandler = routeManager.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);
            }

            Action <IMessage> handler = commandHandler.Delegate;

            handler(command);
            return(true);
        }
Esempio n. 3
0
        public void Send <TCommand>(TCommand command)
            where TCommand : ICommand <TAuthenticationToken>
        {
            Type commandType = typeof(TCommand);

            bool isRequired;

            if (!ConfigurationManager.TryGetSetting(string.Format("{0}.IsRequired", commandType.FullName), out isRequired))
            {
                isRequired = true;
            }

            RouteHandlerDelegate 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));
            }
            else
            {
                Type senderType = commandHandler.TargetedType == null
                                        ? typeof(IConcurrentAkkaCommandSender <>).MakeGenericType(typeof(TAuthenticationToken))
                                        : typeof(IConcurrentAkkaCommandSender <,>).MakeGenericType(typeof(TAuthenticationToken), commandHandler.TargetedType);
                var proxy = (IActorRef)ConcurrentEventBusProxy.Resolve(senderType, command.Id);
                proxy.Tell(command);
            }

            command.Framework = FrameworkType.Akka;
            CommandSender.Send(command);
        }
 public Route(string path, HttpRequestMethod method, RouteHandlerDelegate handler)
 {
     this.Path    = path;
     this.Method  = method;
     this.handler = handler;
     createPathRegex();
 }
Esempio n. 5
0
        public virtual void ReceiveCommand(ICommand <TAuthenticationToken> command)
        {
            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));
                return;
            }

            CorrelationIdHelper.SetCorrelationId(command.CorrelationId);
            AuthenticationTokenHelper.SetAuthenticationToken(command.AuthenticationToken);

            bool isRequired;

            if (!ConfigurationManager.TryGetSetting(string.Format("{0}.IsRequired", commandType.FullName), out isRequired))
            {
                isRequired = true;
            }

            RouteHandlerDelegate 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;
            }

            Action <IMessage> handler = commandHandler.Delegate;

            handler(command);
        }
Esempio n. 6
0
        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("Akka"))
            {
                // if this is the only framework in the list, then it's fine to handle as it's just pre-stamped, if there is more than one framework, then exit.
                if (command.Frameworks.Count() != 1)
                {
                    Logger.LogInfo("The provided command has already been processed in Akka.", 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(true);
        }
 public void AddRoute(string path, HttpRequestMethod method, RouteHandlerDelegate handler)
 {
     routes.Add(new Route(path, method, handler));
 }