IoC Castle Windsor - there is no constructor without parameters for this object

I get a “No parameterless constructor defined for this object” on my controller when the controller and its dependencies are registered accordingly using a (DI / IoC) drawing using Castle Windsor. Can someone take a look at the following and see my mistake, because I do not see it.

enter image description here

Code for registration on global.asax

public class MyApplication : System.Web.HttpApplication { public MvcApplication() { this.container = new WindsorContainer().Install(new DependencyInstaller()); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorActivator(this.container)); } } 

My di

 public class DependencyInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register( 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") .Where(type => type.Name.EndsWith("Repository")).WithServiceAllInterfaces().LifestylePerWebRequest() ); } } 

my controller

 public class PersonController : Controller { private IFirmTasks tasks; public PersonController(IFirmTasks tasks) { this.tasks = tasks; } 

When I download the application, the container loads fine with all the dependencies resolved according to the screenshot when debugging.

enter image description here

Someone has an idea where my flaws lie. Thanks in advance.

+6
asp.net-mvc inversion-of-control ioc-container castle-windsor
source share
1 answer

When I download the application, the container loads fine with all the dependencies resolved according to the screenshot when debugging.

Yes, all of your types are registered.

If you write var controller = container.Resolve<PersonController>(); in Global.asax , you should get an instance of your controller.

Your implementation is valid for Asp.Net WebApi and works only for System.Web.Http.ApiController .

Your code is not working properly because your PersonController inherits from System.Web.Mvc.Controller and System.Web.Mvc.DefaultControllerFactory . You can see it in the first shot.

You must write and enter your custom ControllerFactory in Global.asax in order to create Asp.Net MVC controllers.

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

You can read more in this answer to another SO question .

Here is the default implementation of WindsorControllerFactory :

 /// <summary> /// Controller factory the class is to be used to eliminate hard-coded dependencies /// between controllers and other components /// </summary> public class ControllerFactory : DefaultControllerFactory { private readonly IWindsorContainer container; public WindsorControllerFactory(IWindsorContainer container) { this.container = container; } public override void ReleaseController(IController controller) { container.Release(controller.GetType()); } protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) { return (IController)container.Resolve(controllerType); } } 
+8
source share

All Articles