我目前正致力于在我的应用程序中实现新的 ASP.NET MVC 5 开箱即用身份验证。但是,当使用 Unity 作为我的 IoC 时,我无法使用 AccountController 的任何部分,因为出现错误:
The type IUserStore`1 does not have an accessible constructor.
这是我给定的统一设置,在 global.asax 中调用
public class DependencyConfig
{
public static void Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
container.RegisterType<IEmployeeRepository, EmployeeRepository>();
container.RegisterType<ITeamRepository, TeamRepository>();
container.RegisterType<ICompanyRepository, CompanyRepository>();
return container;
}
}
这里是新的 AccountController.cs 的默认构造函数
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new BusinessTrackerUsersContext())))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
}
这里是在 AccountController 构造函数中调用的项目。这些是具有新名称的默认值。
public class BusinessTrackerUsersContext : IdentityDbContext<ApplicationUser>
{
public BusinessTrackerUsersContext()
: base("DefaultConnection")
{
}
}
public class ApplicationUser : IdentityUser
{
}
我们将不胜感激任何帮助!
最佳答案
我同意 Wiktor 的观点。
不过,您可以向 Unity 注册无参数构造函数,并通过执行以下操作停止它采用更长的参数:
container.RegisterType<AccountController>(new InjectionConstructor());
关于c# - 如何向 Unity IoC 添加 MVC 5 身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20023065/