Beispiel #1
0
        public static ICommandDispatcher Configure(ICommandHandlerProvider commandHandlerProvider,
                                                   string dispatcherName)
        {
            commandHandlerTypes = commandHandlerProvider.GetCommandHandlers();

            ICommandDispatcher commandDispatcher = EAppRuntime.Instance.CurrentApp.ObjectContainer.Resolve <ICommandDispatcher>(dispatcherName);

            if (commandHandlerTypes != null)
            {
                MethodInfo registerMethod = commandDispatcher.GetType().GetMethod("Register", BindingFlags.Instance | BindingFlags.Public);

                foreach (KeyValuePair <Type, Type> commandHandlerPair in commandHandlerTypes)
                {
                    if (!EAppRuntime.Instance.CurrentApp.ObjectContainer.Registered(commandHandlerPair.Value))
                    {
                        EAppRuntime.Instance.CurrentApp.ObjectContainer.RegisterType(commandHandlerPair.Value);
                    }

                    MethodInfo genericRegisterMethod = registerMethod.MakeGenericMethod(commandHandlerPair.Key);

                    genericRegisterMethod.Invoke(commandDispatcher,
                                                 new object[] { EAppRuntime.Instance.CurrentApp.ObjectContainer.Resolve(commandHandlerPair.Value) });
                }
            }

            return(commandDispatcher);
        }
Beispiel #2
0
        private static void CreateCommandHandlers(ICommandDispatcher commandDispatcher,
                                                  ICommandHandlerProvider commandHandlerProvider)
        {
            commandHandlerTypes = commandHandlerProvider.GetHandlers();

            if (commandHandlerTypes != null)
            {
                MethodInfo registerMethod = commandDispatcher.GetType().GetMethod("Register", BindingFlags.Instance | BindingFlags.Public);

                foreach (KeyValuePair <Type, Type> commandHandlerPair in commandHandlerTypes)
                {
                    if (!AppRuntime.Instance.CurrentApplication.ObjectContainer.Registered(commandHandlerPair.Value))
                    {
                        AppRuntime.Instance.CurrentApplication.ObjectContainer.RegisterType(commandHandlerPair.Value);
                    }

                    MethodInfo genericRegisterMethod = registerMethod.MakeGenericMethod(commandHandlerPair.Key);

                    genericRegisterMethod.Invoke(commandDispatcher,
                                                 new object[]
                    {
                        AppRuntime.Instance.CurrentApplication.ObjectContainer.Resolve(commandHandlerPair.Value)
                    });
                }
            }
        }
Beispiel #3
0
        private ActionResult Command(string payload, Type type)
        {
            object command = formatters.Command.Deserialize(type, payload);

            MethodInfo methodInfo = commandDispatcher.GetType().GetMethod(nameof(commandDispatcher.HandleAsync));

            if (methodInfo != null)
            {
                methodInfo = methodInfo.MakeGenericMethod(type);
                Task task = (Task)methodInfo.Invoke(commandDispatcher, new[] { command });
                task.Wait();

                return(Ok());
            }

            return(StatusCode(500));
        }
Beispiel #4
0
        public ActionResult Command([FromBody] Request request)
        {
            string payload = request.Payload;
            Type   type    = Type.GetType(request.Type);
            object command = formatters.Command.Deserialize(type, payload);

            MethodInfo methodInfo = commandDispatcher.GetType().GetMethod(nameof(commandDispatcher.HandleAsync));

            if (methodInfo != null)
            {
                methodInfo = methodInfo.MakeGenericMethod(type);
                Task task = (Task)methodInfo.Invoke(commandDispatcher, new[] { command });
                task.Wait();

                return(Ok());
            }

            return(StatusCode(500));
        }
        public async Task <CommandResult <TResult> > DispatchAsync <TResult>(ICommand <TResult> command, CancellationToken cancellationToken)
        {
            await new SynchronizationContextRemover();

            ICommandDispatchContext dispatchContext = _commandScopeManager.Enter();

            try
            {
                CommandResult <TResult> dispatchResult        = null;
                CommandResult           wrappedDispatchResult = null;
                ICommandExecuter        executer   = null;
                ICommandDispatcher      dispatcher = null;
                ICommand unwrappedCommand          = command;
                if (command is NoResultCommandWrapper wrappedCommand)
                {
                    unwrappedCommand = wrappedCommand.Command;
                }

                // we specifically audit BEFORE dispatch as this allows us to capture intent and a replay to
                // occur even if dispatch fails
                // (there is also an audit opportunity after execution completes and I'm considering putting one in
                // on dispatch success)
                await _auditor.AuditPreDispatch(unwrappedCommand, dispatchContext, cancellationToken);

                try
                {
                    Stopwatch stopwatch = _collectMetrics ? Stopwatch.StartNew() : null;
                    Func <ICommandDispatcher> dispatcherFunc = _commandRegistry.GetCommandDispatcherFactory(command);
                    if (dispatcherFunc != null)
                    {
                        dispatcher = dispatcherFunc();
                        if (command != unwrappedCommand)
                        {
                            wrappedDispatchResult = await dispatcher.DispatchAsync(unwrappedCommand, cancellationToken);
                        }
                        else
                        {
                            dispatchResult = await dispatcher.DispatchAsync(command, cancellationToken);
                        }
                        executer = dispatcher.AssociatedExecuter;
                    }
                    await _auditor.AuditPostDispatch(unwrappedCommand, dispatchContext, stopwatch?.ElapsedMilliseconds ?? -1, cancellationToken);

                    if ((dispatchResult != null && dispatchResult.DeferExecution) || (wrappedDispatchResult != null && wrappedDispatchResult.DeferExecution))
                    {
                        return(new CommandResult <TResult>(default(TResult), true));
                    }
                }
                catch (Exception ex)
                {
                    throw new CommandDispatchException(unwrappedCommand, dispatchContext.Copy(), dispatcher?.GetType() ?? GetType(), "Error occurred during command dispatch", ex);
                }

                if (executer == null)
                {
                    executer = AssociatedExecuter;
                }
                return(new CommandResult <TResult>(await executer.ExecuteAsync(command, cancellationToken), false));
            }
            finally
            {
                _commandScopeManager.Exit();
            }
        }
        public GetServiceHandler(ICommandDispatcher Dispatcher, ILogger Logger)
        {
            Dispatcher.IsNotNull($"Invalid parameter received in the {nameof(GetServiceHandler)} constructor. {nameof(Dispatcher)}");
            Contracts.IsTrue(Dispatcher is ServicePublisher, $"Expected a {nameof(XFS4IoTServer.ServicePublisher)} got {Dispatcher.GetType().FullName}");
            Logger.IsNotNull($"Invalid parameter received in the {nameof(GetServiceHandler)} constructor. {nameof(Logger)}");

            this.ServicePublisher = (ServicePublisher)Dispatcher;
            this.Logger           = Logger;
        }