问题
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: }
回答1:
The S#arpArchitecture project has a decent implentation of using Windsor as a DI framework (for controllers or anything else), 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));
}
回答2:
MvcContrib offers some extension methods for registering controllers in Windsor, example:
windsorContainer.RegisterControllers(Assembly.GetExecutingAssembly());
Mannish's code uses the same extension method.
回答3:
Don't forget the ReleaseController function at the WindsorControllerFactory otherwise your workingprocess will run out of memory because the controllers are not released.
来源:https://stackoverflow.com/questions/896337/need-help-with-castle-windsor-as-an-asp-net-mvc-controllerfactory-please