使用简单注入器和 Web API 解析 Entity Framework 的 AccountController

Resolving Entity Framework's AccountController with Simple Injector and Web API

由于 asp .net Web api 中 ApplicationUserManager、UserStore 的依赖性,AccountController 实例化存在问题。我能够为简单的注入器 DI 成功注册所有其他依赖项。

我已经搜索了很多地方来注入正确的东西instances/types,但还不能最终正确地做到这一点。

有没有办法让我忽略简单注入器提供的默认 RegisterWebApiControllers,这样我就可以避免通过简单注入器实例化 AccountController?不切实际吗?

只需删除 AccountController 的默认构造函数。就这么简单。

另一种选择是覆盖 AccountController 注册,如下所示:

container.RegisterWebApiControllers(config);

container.Options.AllowOverridingRegistrations = true;

container.Register<AccountController>(() => new AccountController());

container.Options.AllowOverridingRegistrations = false;

以下是将 Identity 与 Simple Injector 结合使用时的一些有趣读物: