Need help with Castle.Windsor as Asp.Net MVC ControllerFactory please
I'm trying to implement Steven Sanderson's WinsorControllerFactory from his book Asp.Net MVC Framework (great book by the way) and I'm stumbling over the problem. I'm not sure what else you need to know in order to formulate an answer, but I really appreciate any help on this. Thanks!
Here's the code:
WindsorControllerFactory
public class WindsorControllerFactory : DefaultControllerFactory
{
private WindsorContainer _container;
public WindsorControllerFactory()
{
_container= new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
where typeof (IController).IsAssignableFrom(t)
select t;
foreach(Type t in controllerTypes)
{
_container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
}
}
protected override IController GetControllerInstance(Type controllerType)
{
return (IController)_container.Resolve(controllerType);
}
}
Web.Config
<castle>
<components>
<component id="MenuRepository"
service="****.IMenuRepository, ****.Model"
type="****.FakeMenuRepository, ****.Model">
</component>
<component id="NewsRepository"
service="****.INewsRepository, ****.Model"
type="****.FakeNewsRepository, ****.Model">
</component>
</components>
</castle>
NewsArticleController
public class NewsArticleController : Controller
{
private INewsRepository _repository { get; set; }
public NewsArticleController(INewsRepository repository)
{
_repository = repository;
}
Global.asax
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory((new WindsorControllerFactory()));
}
ERROR MESSAGE There is no component to support the service ****. NewsArticleController was found Description: An unhandled exception was thrown during the execution of the current web request. Review the stack trace for more information on the error and where it occurred in your code.
Exception Details: Castle.MicroKernel.ComponentNotFoundException: No component for supporting the service ****.NewsArticleController was found
Source Error:
Line 29: protected override IController GetControllerInstance(Type controllerType)
Line 30: {
Line 31: return (IController)_container.Resolve(controllerType);
Line 32: }
Line 33: }
a source to share
The S arpArchitecture project has a decent implementation of Windsor as a DI framework (for controllers or whatever), with little or no need to add web.config sections - http://code.google.com/p/sharp-architecture/
Code examples:
CastleWindsor / ComponentRegistrar.cs:
public class ComponentRegistrar
{
public static void AddComponentsTo(IWindsorContainer container) {
AddGenericRepositoriesTo(container);
AddCustomRepositoriesTo(container);
container.AddComponent("validator",
typeof(IValidator), typeof(Validator));
}
private static void AddCustomRepositoriesTo(IWindsorContainer container) {
container.Register(
AllTypes.Pick()
.FromAssemblyNamed("Northwind.Data")
.WithService.FirstNonGenericCoreInterface("Northwind.Core"));
}
private static void AddGenericRepositoriesTo(IWindsorContainer container) {
container.AddComponent("entityDuplicateChecker",
typeof(IEntityDuplicateChecker), typeof(EntityDuplicateChecker));
container.AddComponent("repositoryType",
typeof(IRepository<>), typeof(Repository<>));
container.AddComponent("nhibernateRepositoryType",
typeof(INHibernateRepository<>), typeof(NHibernateRepository<>));
container.AddComponent("repositoryWithTypedId",
typeof(IRepositoryWithTypedId<,>), typeof(RepositoryWithTypedId<,>));
container.AddComponent("nhibernateRepositoryWithTypedId",
typeof(INHibernateRepositoryWithTypedId<,>), typeof(NHibernateRepositoryWithTypedId<,>));
}
}
Global.asax (primary initialization method for di):
protected virtual void InitializeServiceLocator() {
IWindsorContainer container = new WindsorContainer();
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));
container.RegisterControllers(typeof(HomeController).Assembly);
ComponentRegistrar.AddComponentsTo(container);
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));
}
a source to share