Asp.net mvc 没有使用SimpleInjector APIv3注册类型ICommandHandler

Asp.net mvc 没有使用SimpleInjector APIv3注册类型ICommandHandler,asp.net-mvc,dependency-injection,cqrs,simple-injector,Asp.net Mvc,Dependency Injection,Cqrs,Simple Injector,我一直在使用SimpleInjector,我正在尝试正确注册所有命令处理程序 这是我的密码: CQRS.cs Global.asax.cs 在CQRS.cs dynamic handler = container.GetInstance(handlerType); 我得到: 找不到类型ICommandHandler的注册。 但是,IEnumerable; 您是想调用GetAllInstances()还是依赖于IEnumerable 简单的注入器API提供了清晰的一对一映射。在合成根目录中,您正

我一直在使用SimpleInjector,我正在尝试正确注册所有命令处理程序

这是我的密码:

CQRS.cs Global.asax.cs 在CQRS.cs

dynamic handler = container.GetInstance(handlerType);
我得到:

找不到类型
ICommandHandler
的注册。 但是,
IEnumerable
; 您是想调用
GetAllInstances()
还是依赖于
IEnumerable


简单的注入器API提供了清晰的一对一映射。在合成根目录中,您正在进行以下注册:

container.RegisterCollection(typeof(ICommandHandler<>), 
    new[] { typeof(ICommandHandler<>).Assembly });

按建议更改:I get:找不到类型ICommandHandler的注册。@R2D2:确保将包含命令处理程序的程序集提供给
Register
方法,并且命令处理程序实现
ICommandHandler
(并确保您没有意外地定义第二个
ICommandHandler
抽象)。如果这不起作用,请发布您的更新配置(将其附加到您的问题中)。您所做的是非常正常的,并且有许多简单的注入器用户(包括我自己)每天都这样做,所以这一定很愚蠢。你是对的。这真的很愚蠢。我所有的处理程序都位于不同于ICommandHandler的程序集中。@R2D2:顺便说一句,我建议不要使用
CommandDispatcher
,而是直接将
ICommandHandler
注入控制器中。这会使更容易获得。如果您看到控制器注入了许多命令处理程序,这表明您的控制器做得太多,违反了单一责任原则。
public class CreateUser : ICommand
{
    public readonly string Email;

    public CreateUser(string email)
    {
        Email = email;
    }       
}
var assemblies = new[] { typeof(ICommandHandler<>).Assembly };
var container = new SimpleInjector.Container();
container.RegisterCollection(typeof(ICommandHandler<>), assemblies);
container.RegisterSingleton<ICommandDispatcher>(new CommandDispatcher(container));
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
public class HomeController : Controller
{
    private readonly ICommandDispatcher _commandDispatcher;

    public HomeController(ICommandDispatcher commandDispatcher)
    {
        _commandDispatcher = commandDispatcher;
    }

    public ActionResult Index()
    {
        var command = new CreateUser("email@example.com");
        _commandDispatcher.Execute(command);
        return Content("It works");
    }
}
dynamic handler = container.GetInstance(handlerType);
container.RegisterCollection(typeof(ICommandHandler<>), 
    new[] { typeof(ICommandHandler<>).Assembly });
// Use 'Register' instead of 'RegisterCollection'.
container.Register(typeof(ICommandHandler<>), 
    new[] { typeof(ICommandHandler<>).Assembly });