我創建一個CQRS-ES測試應用程序。我用下面的代碼掃描命令處理程序對命令沒有處理程序註冊CQRS-ES
public class StructureMapCommandHandlerFactory : ICommandHandlerFactory
{
public ICommandHandler<T> GetHandler<T>() where T : Command
{
var handlers = GetHandlerTypes<T>().ToList();
var cmdHandler = handlers.Select(handler =>
(ICommandHandler<T>)ObjectFactory.GetInstance(handler)).FirstOrDefault();
return cmdHandler;
}
private IEnumerable<Type> GetHandlerTypes<T>() where T : Command
{
var handlers = typeof(ICommandHandler<>).Assembly.GetExportedTypes()
.Where(x => x.GetInterfaces()
.Any(a => a.IsGenericType && a.GetGenericTypeDefinition() == typeof(ICommandHandler<>)))
.Where(h => h.GetInterfaces()
.Any(ii => ii.GetGenericArguments()
.Any(aa => aa == typeof(T)))).ToList();
return handlers;
}
}
我調度命令,如下所示:
public class CommandBus:ICommandBus
{
private readonly ICommandHandlerFactory _commandHandlerFactory;
public CommandBus(ICommandHandlerFactory commandHandlerFactory)
{
_commandHandlerFactory = commandHandlerFactory;
}
public void Send<T>(T command) where T : Command
{
var handler = _commandHandlerFactory.GetHandler<T>();
if (handler != null)
{
handler.Execute(command);
}
else
{
throw new UnregisteredDomainCommandException("no handler registered");
}
}
}
當我運行該項目,它拋出一個異常 「無處理程序註冊」
我的項目位置是https://drive.google.com/file/d/0B1rU7HOTfLwea0tVOXdIb2Nib2M/edit?usp=sharing
的這個異常如下的棧跟蹤: [UnregisteredDomainCommandException:無註冊處理程序] f:\ Video \ Latest Readable中的CQRS.Infrastructure.Messaging.CommandBus.Send(T命令)\ CQRS教程\ CQRS作者\ HRMSystem \ CQRS.Infrastructure \ Messaging \ CommandBus.cs :31 HRMSWeb.Facade.DiaryItemFacade.Delete(Guid id)in f:\ Video \ Latest Readable \ CQRS Tutorial \ CQRS By Authors \ HRMSystem \ HRMSWeb \ Facade \ DiaryItemFacade.cs:59 HRMSWeb.Controllers.HomeController.Delete( GUID ID)在F:\視頻\最新讀\ CQRS教程\ CQRS通過作者\ HRMSystem \ HRMSWeb \ \控制器HomeController.cs:29 lambda_method(封閉,ControllerBase,對象[])184 System.Web.Mvc。 ActionMethodDispatcher.Execute(ControllerBase控制器,Object []參數)+14 System.Web.Mvc.ReflectedActionDescriptor.Execute(Contr ollerContext controllerContext,IDictionary 2 parameters) +211 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters)+27 System.Web.Mvc.Async。 <> c_ DisplayClass42.b _41()+28 System.Web.Mvc.Async。 <> c_ DisplayClass8 1.<BeginSynchronous>b__7(IAsyncResult _) +10 System.Web.Mvc.Async.WrappedAsyncResult
1.End()+57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+48 System.Web.Mvc.Async。 <> c _DisplayClass39.b_ 33()+57 System.Web.Mvc.Async。 <> c _DisplayClass4f.b_ 49()+223 System.Web.Mvc.Async。 <>Ç _DisplayClass37.b_ 36(IAsyncResult的asyncResult)10 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48 System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24 System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102 System.Web.Mvc.Async.WrappedAsyncResult
1.End()57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult的asyncResult)+ 43 System.Web.Mvc。 <>Ç _DisplayClass1d.b_ 18(IAsyncResult的asyncResult)14 System.Web.Mvc.Async。 <>Ç _DisplayClass4.b_ 3(IAsyncResult的AR)23 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +62 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult
1.End()62 System.Web.Mvc.Controller.EndExecute(IAsyncResult的asyncResult)47 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)+10 System.Web.Mvc。 <> c _DisplayClass8.b_ 3(IAsyncResult asyncResult)+25 System.Web.Mvc.Async。 <>Ç _DisplayClass4.b__3(IAsyncResult的AR)23 System.Web.Mvc.Async.WrappedAsyncResult`1.End()62 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult的asyncResult)47 系統。 Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult的結果)+9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep。執行()9629708 System.Web.HttpApplication.ExecuteStep(IExecutionStep一步,布爾& completedSynchronously)155