您好,我一直在尝试使用 SimpleInjector 注册预制 Identity 类 ApplicationUserManager 中的默认 IUserStore
public class ApplicationUserManager : UserManager<ApplicationUser, int>
{
public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
: base(store)
{
}
我在 App_Start 文件夹中有一个 SimpleInjectorInitializer 文件,如下所示:
public static class SimpleInjectorInitializer
{
public static void RegisterDependencies(Container container)
{
container.Register<ICatalogRepository, CatalogRepository>();
container.Register<IUserRepository, UserRepository>();
container.Register<ICatalogService, CatalogService>();
container.Register<IUserService, UserService>();
container.Register<IAdminService, AdminService>();
container.Register<AccountController>();
container.Register<ApplicationUserManager>();
//container.Register<CustomUserStore>();
//container.RegisterCollection(typeof(IUserStore<>), new[] { typeof(ApplicationUser) });
//container.Register<IUserStore<>, store>();
//container.RegisterPerWebRequest<IUserStore<ApplicationUser>>(() => new UserStore<ApplicationUser>(container.GetInstance<ApplicationDbContext>()));
container.Verify();
}
}
正如你所看到的,我已经尝试了几次通过查看类似的 Stack Overflow 问题来让它工作,但它要么没有改变结果,要么给出了语法错误。
目前,当点击应用主页上的登录按钮时,会抛出此错误:
The constructor of type ApplicationUserManager contains the parameter with name 'store' and type IUserStore that is not registered. Please ensure IUserStore is registered, or change the constructor of ApplicationUserManager.
如果注册ApplicationUserManager下面的两行未注释,则会抛出此错误:
An exception of type 'System.ArgumentException' occurred in SimpleInjector.dll but was not handled in user code
Additional information: The supplied type ApplicationUser does not implement IUserStore.
对此的任何帮助将不胜感激。
最佳答案
我回答了我自己的问题。我使用了各种其他堆栈溢出问题,答案是编辑我的 SimpleInjectorInitializer,如下所示:
public static class SimpleInjectorInitializer
{
public static void RegisterDependencies(Container container)
{
container.Register<ICatalogRepository, CatalogRepository>();
container.Register<IUserRepository, UserRepository>();
container.Register<ICatalogService, CatalogService>();
container.Register<IUserService, UserService>();
container.Register<IAdminService, AdminService>();
container.RegisterPerWebRequest<IUserStore<ApplicationUser, int>>(
() => new CustomUserStore(container.GetInstance<ApplicationDbContext>()));
container.RegisterPerWebRequest(
() => new UserManager<ApplicationUser, int>(
new CustomUserStore(
container.GetInstance<ApplicationDbContext>())));
container.Register(
() => HttpContext.Current.GetOwinContext().Authentication);
}
}
注意:container.Verify()
将阻止此解决方案工作,但它确实有效。我需要做进一步的研究,以找到一种更好的方法,同时使用 container.Verify()
。
关于c# - 使用简单注入(inject)器 3.1.2 注册 IUserStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295878/