Unity IOC 无法修复无参数构造函数问题
本文关键字:参数 构造函数 问题 IOC Unity | 更新日期: 2023-09-27 18:34:36
我有以下构造函数(控制器只有一个(:
[Authorize]
public class AccountController : Controller
{
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager,
IAccountService accountSrv)
{
UserManager = userManager;
SignInManager = signInManager;
AccountService = accountSrv;
}
private IAccountService AccountService { get; }
private ApplicationSignInManager SignInManager { get; }
private ApplicationUserManager UserManager { get; }
我希望 MVC 使用适当的参数调用它,所以我尝试了这个:
全球.asax.cs
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
DALMapper.Mapping();
LabMapper.Mapping();
var container = new UnityContainer();
container
.RegisterType<IDALContext, DALContext>()
.RegisterType<IConsultantService, ConsultantService>()
.RegisterType<IProjectService, ProjectService>()
.RegisterType<IAccountService, AccountService>()
.RegisterType<AccountController, AccountController>()
.RegisterType<ApplicationUserManager, ApplicationUserManager>()
.RegisterType<ApplicationSignInManager, ApplicationSignInManager>()
.RegisterInstance<IAccountService>(new AccountService(new DALContext()), new ContainerControlledLifetimeManager())
;
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
(UnityDependencyResolver being:(
public class UnityDependencyResolver : IDependencyResolver
{
private readonly IUnityContainer _container;
public UnityDependencyResolver(IUnityContainer container)
{
_container = container;
}
public object GetService(Type serviceType)
{
try
{
return _container.Resolve(serviceType);
}
catch
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return _container.ResolveAll(serviceType);
}
catch
{
return new List<object>();
}
}
}
我在哪里注册了所需的每种类型(尽管当 in/from 类型相同时,结果感觉很奇怪(我是 Unity 的新手((。
但它不起作用。(据我所知,从 MV3 开始,默认控制器工厂将使用服务定位器,因此不需要实现它自己的工厂(。
怎么了?
当前结果:
Server Error in '/' Application.
No parameterless constructor defined for this object.
如果我
正确理解您的问题,则解析应用程序用户管理器和应用程序登录管理器的依赖项时出现问题。这是为我解决问题的代码:
//for accountcontroller
container.RegisterType<DbContext, ApplicationDBContext>(new HierarchicalLifetimeManager());
container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
container.RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication));
在挖掘更多(下载 Unity 源代码、构建它、添加对 dll 的引用、将 pdb 添加到符号位置(调试((*(* 之后,我可以看到错误来自另一个需要参数的构造函数。
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
(我还没有解决这个问题,但这应该很容易(。
*有没有更简单的方法可以进入第三方库的代码?在评论中让我知道。