I'm trying to implement Steven Sanderson's WinsorControllerFactory from his pro Asp.Net MVC Framework book (great book, btw) and I'm stumbling into an issue. I'm not sure what else you'll need to know in order to formulate a response but I greatly appreciate any help in 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 No component for supporting the service ****.NewsArticleController was found Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the 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: }