Unity IOC,无法修复无参数构造函数问题
Unity IOC, can't fix the parameterless constructor issue
我有以下构造函数(控制器只有一个):
[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 使用适当的参数调用它,所以我尝试了这个:
Global.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 是:)
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.
深入挖掘后(下载 Unity 源代码,构建它,添加对 dll 的引用,将 pdb 添加到符号位置(调试))*,我可以看到错误来自另一个构造函数需要参数。
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
(我还没有解决这个问题,但这应该很容易)。
*是否有更简单的方法进入第三方库的代码?在评论中让我知道。
如果我对你的问题的理解正确,那么在解决 ApplicationUserManager 和 ApplicationSignInManager 的依赖关系时会遇到问题。
这是为我解决问题的代码:
//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));
我有以下构造函数(控制器只有一个):
[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 使用适当的参数调用它,所以我尝试了这个:
Global.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 是:)
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.
深入挖掘后(下载 Unity 源代码,构建它,添加对 dll 的引用,将 pdb 添加到符号位置(调试))*,我可以看到错误来自另一个构造函数需要参数。
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
(我还没有解决这个问题,但这应该很容易)。
*是否有更简单的方法进入第三方库的代码?在评论中让我知道。
如果我对你的问题的理解正确,那么在解决 ApplicationUserManager 和 ApplicationSignInManager 的依赖关系时会遇到问题。 这是为我解决问题的代码:
//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));