No components were found to support the service. System.Web.Mvc.IControllerFactory

I have a problem in Castle Windsor. Please help me! I am using Castle Windsor 3.2.0.0, .Net 4.0 and Mvc 3.0.0.0

Thank you very much!

Question: No components were found to support the Mvc IControllerFactory service. There is code here.

public class WindsorDependencyResolver : IDependencyResolver { private readonly IWindsorContainer _container; public WindsorDependencyResolver(IWindsorContainer container) { this._container = container; } public object GetService(Type serviceType) { return _container.Resolve(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { return _container.ResolveAll(serviceType).Cast<object>(); //return _container.ResolveAll<object>(serviceType); } } //Castle Windsor register protected void Application_Start() { var container = new WindsorContainer(); container.AddFacility<TypedFactoryFacility>() .Register(Component.For<IServiceFactory>().AsFactory().LifeStyle.Transient); //Do register the service/type to container container.Register( Types.FromThisAssembly() .BasedOn(typeof(Controller)) .WithService.Self().LifestyleTransient()); container.Register( Component.For<IRepository>() .ImplementedBy<GenericRepository>().DependsOn(Dependency.OnValue("context", "ProjectContext")) .LifeStyle.PerWebRequest); container.Register( Types.FromAssembly(Assembly.GetAssembly(typeof(IUserService))) .BasedOn(typeof(ServiceBase)) .WithService.DefaultInterfaces().LifestylePerWebRequest()); DependencyResolver.SetResolver(new WindsorDependencyResolver(container)); } 
+2
source share
2 answers

I agreed with Cristiano - this is how I did it, but it was a combination of both:

 public class ContainerHostDependencyResolver : IDependencyResolver { private static readonly IServiceLocator Locator = new ServiceLocator(); private readonly IDependencyResolver _defaultResolver; private static readonly Type ControllerFactoryType = typeof (IControllerFactory); public ContainerHostDependencyResolver(IDependencyResolver defaultResolver) { _defaultResolver = defaultResolver; } public object GetService(Type serviceType) { if (ControllerFactoryType.IsAssignableFrom(serviceType)) return Locator.Resolve(serviceType); return _defaultResolver.GetService(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { return _defaultResolver.GetServices(serviceType); } } protected void Application_Start() { DependencyResolver.SetResolver(new ContainerHostDependencyResolver(DependencyResolver.Current)); } 

EDIT # 1: This is a safer, simpler approach that does what the reference to the Cristiano tutorial requires. In my case, I have a Windsor wrapped behind our own name ContainerHost / ServiceLocator.

 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { ControllerBuilder.Current.SetControllerFactory(new ContainerHostControllerFactory()); } } public class ContainerHostControllerFactory : DefaultControllerFactory { private static readonly IServiceLocator Locator = new ServiceLocator(); public override IController CreateController(RequestContext requestContext, string controllerName) { return Locator.Resolve<IController>(controllerName + "Controller"); } public override void ReleaseController(IController controller) { Locator.Release(controller); } } 
+1
source

Stay away from DependencyResolver ... use ControllerFactory instead.

Take a look at the tutorial

+4
source

Source: https://habr.com/ru/post/951944/


All Articles