0
使用MVC并试图为控制器使用依赖项注入,但是当我尝试调用一个控制器上的方法时,我得到了“无参数构造函数“错误。这里是我的设置:“没有无参数的构造函数”错误实例化控制器注册W/Windsor容器
ProductRepository : IProductRepository
ProductService : IProductService {
public ProductService(IProductRepository repository) {} }
ProductController {
public ProductController(IProductService service) {} }
在Global.asax中:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
InitializeServiceLocator();
RegisterRoutes(RouteTable.Routes);
}
protected virtual void InitializeServiceLocator()
{
IWindsorContainer container = new WindsorContainer();
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));
container.RegisterControllers(typeof(HomeController).Assembly);
ComponentRegistrar.AddComponentsTo(container);
foreach (var handler in container.Kernel.GetAssignableHandlers(typeof(object)))
{
System.Diagnostics.Debug.WriteLine(String.Format("{0} {1}",
handler.ComponentModel.Service,
handler.ComponentModel.Implementation));
}
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));
}
ComponentRegistrar:
public static void AddComponentsTo(IWindsorContainer container)
{
AddCustomRepositoriesTo(container);
AddApplicationServicesTo(container);
}
当InitializeServiceLocator完成后,我可以看到,所有控制器,服务和存储库注册。
任何帮助非常感谢。
我知道在Ninject中你必须在服务定位器中注册参数类型。你可能需要做一些类似于温莎的事情 – Buildstarted 2010-09-20 02:10:21