asp.net-mvc – IoC Castle Windsor – 没有为此对象定义的无参数构造函数

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – IoC Castle Windsor – 没有为此对象定义的无参数构造函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当使用Castle Windsor通过(DI / IoC)模式相应地注册控制器及其依赖关系时,我在控制器上获得了“为此对象定义的无参数构造函数”.有人可以看看下面的内容,看看我的错误,因为我看不到它.

在global.asax上注册代码

  1. public class MyApplication : System.Web.HttpApplication
  2. {
  3. public MvcApplication()
  4. {
  5. this.container = new WindsorContainer().Install(new DependencyInstaller());
  6. }
  7. protected void Application_Start()
  8. {
  9. AreaRegistration.RegisterAllAreas();
  10.  
  11. WebApiConfig.Register(GlobalConfiguration.Configuration);
  12. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  13. RouteConfig.RegisterRoutes(RouteTable.Routes);
  14. BundleConfig.RegisterBundles(BundleTable.Bundles);
  15. GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator),new WindsorActivator(this.container));
  16. }
  17. }

我的DI

  1. public class DependencyInstaller : IWindsorInstaller
  2. {
  3. public void Install(IWindsorContainer container,IConfigurationStore store)
  4. {
  5. container.Register(
  6. Component.For<IDatabaseFactory>().ImplementedBy<DatabaseFactory>().LifeStyle.PerWebRequest,Component.For<IUnitOfWork>().ImplementedBy<UnitOfWork>().LifeStyle.PerWebRequest,Component.For<IMappingEngine>().ImplementedBy<MappingEngine>().UsingFactoryMethod(() => Mapper.Engine).LifeStyle.Singleton,Component.For<IFirmTasks>().ImplementedBy<FirmTasks>().LifeStyle.PerWebRequest,Classes.FromAssemblyContaining<PersonController>().BasedOn<IController>().LifestyleTransient(),Classes.FromThisAssembly().BasedOn<IHttpController>().LifestyleTransient(),Classes.FromAssemblyNamed("Core.Firm.Tasks").Where(type => type.Name.EndsWith("Tasks")).WithServiceAllInterfaces().LifestylePerWebRequest(),Classes.FromAssemblyNamed("Core.Firm.Repository")
  7. .Where(type => type.Name.EndsWith("Repository")).WithServiceAllInterfaces().LifestylePerWebRequest()
  8. );
  9. }
  10. }

我的控制器

  1. public class PersonController : Controller
  2. {
  3. private IFirmTasks tasks;
  4.  
  5. public PersonController(IFirmTasks tasks)
  6. {
  7. this.tasks = tasks;
  8. }

当我加载应用程序时,容器加载正常,所有依赖项按照调试时的屏幕截图解析.

那里的任何人都知道我的失败所在.提前致谢.

解决方法

When I load the application the container loads fine with all dependencies resolved as per screen shot on debug.

是的,您的所有类型都已注册.

如果你写var controller = container.Resolve< PersonController>();在Global.asax中,您应该获得控制器的实例.

您的实现对Asp.Net WebApi有效,它仅适用于System.Web.Http.ApiControllers.

您的代码无法正常工作,因为您的PersonController继承了System.Web.Mvc.Controller并使用了System.Web.Mvc.DefaultControllerFactory.你可以在第一张图片中看到它.

您应该在Global.asax中编写和注入自定义ControllerFactory,以便创建Asp.Net MVC控制器.

  1. //Set the controller builder to use our custom controller factory
  2. var controllerFactory = new WindsorControllerFactory(container);
  3. ControllerBuilder.Current.SetControllerFactory(controllerFactory);

你可以阅读more in this answer on another SO question.

这是WindsorControllerFactory的默认实现:

  1. /// <summary>
  2. /// Controller factory the class is to be used to eliminate hard-coded dependencies
  3. /// between controllers and other components
  4. /// </summary>
  5. public class ControllerFactory : DefaultControllerFactory
  6. {
  7. private readonly IWindsorContainer container;
  8.  
  9. public WindsorControllerFactory(IWindsorContainer container)
  10. {
  11. this.container = container;
  12. }
  13.  
  14. public override void ReleaseController(IController controller)
  15. {
  16. container.Release(controller.GetType());
  17. }
  18.  
  19. protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext,Type controllerType)
  20. {
  21. return (IController)container.Resolve(controllerType);
  22. }
  23. }

猜你在找的asp.Net相关文章