我安装了Unity,并按照这个家伙的示例将依赖项注入到我的mvc应用程序中,Link现在,当我运行我的项目时,我面临以下豁免。
System.InvalidOperationException:'尝试创建一个控制器时发生错误 键入“ SaaS.Controllers.AccountController”。确保控制器具有无参数 公共构造函数
InvalidRegistrationException:类型的异常 抛出了“ Unity.Exceptions.InvalidRegistrationException”。
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private ITenantRepository TenantRepo;
private ISetupRepository SetupRepo;
private IAppUserRepository AppUserRepo;
private IEmployeeRepository EmployeeRepo;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, ITenantRepository TenantRepo,
ISetupRepository SetupRepo, IAppUserRepository AppUserRepo, IEmployeeRepository EmployeeRepo)
{
UserManager = userManager;
SignInManager = signInManager;
this.TenantRepo = TenantRepo;
this.SetupRepo = SetupRepo;
this.AppUserRepo = AppUserRepo;
this.EmployeeRepo = EmployeeRepo;
}
一个仓库
public static void RegisterComponents()
{
var container = new UnityContainer();
container.RegisterType<ISetupRepository, SetupRepository>();
container.RegisterType<ITenantRepository, TenantRepository>();
container.RegisterType<ISetupLocationRepository, SetupLocationRepository>();
container.RegisterType<ISetupCalendarOfficeRepository, SetupCalendarOfficeRepository>();
container.RegisterType<IAppUserRepository, AppUserRepository>();
container.RegisterType<ICacheService, InMemoryCache>();
container.RegisterType<IEmpPersonalDetailRepository, EmpPersonalDetailRepository>();
container.RegisterType<IEmpSportRepository, EmpSportRepository>();
container.RegisterType<IEmployeeRepository, EmployeeRepository>();
container.RegisterType<IEmpContactDetailRepository, EmpContactDetailRepository>();
container.RegisterType<IEmpEmploymentDetailRepository, EmpEmploymentDetailRepository>();
container.RegisterType<IEmpVacationEntitlementRepository, EmpVacationEntitlementRepository>();
container.RegisterType<IEmpCalendarRepository, EmpCalendarRepository>();
container.RegisterType<IEmpSalaryRepository, EmpSalaryRepository>();
container.RegisterType<ISetupSalaryComponantRepository, SetupSalaryComponantRepository>();
container.RegisterType<ISetupPayGradeRepository, SetupPayGradeRepository>();
container.RegisterType<IEmpPartSalaryRepository, EmpPartSalaryRepository>();
container.RegisterType(typeof(IUserStore<ApplicationUser>), typeof(UserStore<ApplicationUser>));
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
public class SetupRepository : ISetupRepository/*, IDisposable*/
{
[Dependency]
public DataBaseContext context { get; set; }
public void Add(Setup entity)
{
context.Setups.Add(entity);
context.SaveChanges();
}
}
答案 0 :(得分:0)
问题是AccountController
构造函数具有用于ApplicationUserManager
和ApplicationSignInManager
的参数,并且这些参数未在您的容器中注册,因此Unity不在从那里获取这些值的位置。如果您注册这两种类型(或实例),那么它应该对您有用。