No route specified for message 'RavenProject.Commands.CreateUserCommand'

64 views
Skip to first unread message

Mojam Dewan

unread,
Apr 11, 2014, 11:25:12 PM4/11/14
to ddd...@googlegroups.com
I am following Mark Nihof (Fohjin) architecture for develop a cqrs based ecommerce application. My development framework is Asp.net MVC5. Reporting side work fine but when i try to execute command by browsing http://localhost:63738/api/Security/Signup it shows me following exception
No route specified for message 'RavenProject.Commands.CreateUserCommand'

My message router class is as follows:
public class MessageRouter : IRouteMessages
    {
        private readonly IDictionary<Type, ICollection<Action<object>>> _routes;

        public MessageRouter()
        {
            _routes = new Dictionary<Type, ICollection<Action<object>>>();
        }

        public void Register<TMessage>(Action<TMessage> route) where TMessage : class
        {
            var routingKey = typeof(TMessage);
            ICollection<Action<object>> routes;

            if (!_routes.TryGetValue(routingKey, out routes))
                _routes[routingKey] = routes = new LinkedList<Action<object>>();

            routes.Add(message => route(message as TMessage));
        }

        public void Route(object message)
        {
            ICollection<Action<object>> routes;

            if (!_routes.TryGetValue(message.GetType(), out routes))
                throw new RouteNotRegisteredException(message.GetType());

            foreach (var route in routes)
                route(message);
        }
    }

and my route register class is as follows:
public class RegisterCommandHandlersInMessageRouter
    {
        private static MethodInfo _createPublishActionWrappedInTransactionMethod;
        private static MethodInfo _registerMethod;

        public static void BootStrap()
        {
            new RegisterCommandHandlersInMessageRouter().RegisterRoutes(ObjectFactory.GetInstance<IRouteMessages>() as MessageRouter);
        }

        public void RegisterRoutes(MessageRouter messageRouter)
        {
            _createPublishActionWrappedInTransactionMethod = GetType().GetMethod("CreatePublishActionWrappedInTransaction");
            _registerMethod = messageRouter.GetType().GetMethod("Register");

            var commands = CommandHandlerFactory.GetCommands();
            var commandHandlers = CommandHandlerFactory.GetCommandHandlers();

            foreach (var command in commands)
            {
                IList<Type> commandHandlerTypes;
                if (!commandHandlers.TryGetValue(command, out commandHandlerTypes))
                    throw new Exception(string.Format("No command handlers found for event '{0}'", command.FullName));

                foreach (var commandHandler in commandHandlerTypes)
                {
                    var injectedCommandHandler = GetCorrectlyInjectedCommandHandler(commandHandler);
                    var action = CreateTheProperAction(command, injectedCommandHandler);
                    RegisterTheCreatedActionWithTheMessageRouter(messageRouter, command, action);
                }
            }
        }

        private static object GetCorrectlyInjectedCommandHandler(Type commandHandler)
        {
            return ObjectFactory.GetInstance(commandHandler);
        }

        private static void RegisterTheCreatedActionWithTheMessageRouter(MessageRouter messageRouter, Type commandType, object action)
        {
            _registerMethod.MakeGenericMethod(commandType).Invoke(messageRouter, new[] { action });
        }

        private object CreateTheProperAction(Type commandType, object commandHandler)
        {
            return _createPublishActionWrappedInTransactionMethod.MakeGenericMethod(commandType, commandHandler.GetType()).Invoke(this, new[] { commandHandler });
        }

        public Action<TCommand> CreatePublishActionWrappedInTransaction<TCommand, TCommandHandler>(TCommandHandler commandHandler)
            where TCommand : class
            where TCommandHandler : ICommandHandler<TCommand>
        {
            //return command => ObjectFactory.GetInstance<TransactionHandler<TCommand, TCommandHandler>>().Execute(command, commandHandler);
            return command => ObjectFactory.GetInstance<ICommandHandler<TCommand>>().Execute(command);
        }
    }

Where is my mistake yet i failed to identify.

One can also check whole my work from following link

Thanks
Mojammel
Message has been deleted

Mojam Dewan

unread,
Apr 13, 2014, 2:26:13 PM4/13/14
to ddd...@googlegroups.com
I am really suffering with this problem. If it is possible please help me to solve the issue. I can't fix where is the problem. 

Yin Zhang

unread,
Apr 14, 2014, 5:44:03 AM4/14/14
to ddd...@googlegroups.com
You need to understand your IOC container. Look at the registration of  x.For<IRouteMessages>().Use<MessageRouter>();

Once you fixed the above line, you will have more problems with resolving ICommandHandler. (it can find by type, but cannot resolve).

I think the best place to ask those questions are in the StructMap group since this is no longer CQRS / DDD related questions. : )
Reply all
Reply to author
Forward
0 new messages