一旦我试图解决我的 unitOfWork 我得到这个错误:
“IUnitOfWork 类型没有可访问的构造函数。”
然而,这只发生在我将 unitOfWork 的 LifetimeManager 设置为 PerResolveLifetimeManager 时。如果我只使用默认的,一切正常。我的 unitOfWork,确实有一个公共的无参数构造函数。这是我的代码:
//Global asax
IUnityContainer unity = new UnityContainer();
unity.RegisterType<HomeController>();
unity.RegisterInstance<IUnitOfWork>(new UnitOfWork(), new PerResolveLifetimeManager());
ControllerBuilder.Current.SetControllerFactory(new IocControllerFactory(unity));
//IocControllerFactory
public class IocControllerFactory : DefaultControllerFactory
{
private readonly IUnityContainer _container;
public IocControllerFactory(IUnityContainer container)
{
_container = container;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType != null)
return _container.Resolve(controllerType) as IController;
else
return base.GetControllerInstance(requestContext, controllerType);
}
}
//Home controller constructor
public HomeController(IUnitOfWork unitOfWork)
{
}