0
am遵循Mark Nihof(Fohjin)架構開發基於cqrs的電子商務應用程序。我的開發框架是Asp.net MVC5。報表方面做工精細,但當我試着通過瀏覽執行命令本地主機:63738/API /安全/註冊它讓我看到下面的異常 沒有找到消息中指定的路線「RavenProject.Commands.CreateUserCommand」沒有指定郵件的路由
我的消息路由器類如下:
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);
}
}
和我的路線寄存器類別如下:
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);
}
}
哪裏是我的錯誤,但我未能確定。
注意:我從瀏覽器調用的方法實際上是一個HTTP POST方法,但爲了檢查目的,我使用了HTTP GET。
我的路由註冊類在引導應用程序期間正確地註冊所有路由。
你也可以從下面的鏈接 https://drive.google.com/file/d/0B1rU7HOTfLweZjFuZlF3M0Z2M28/edit?usp=sharing