我正在尝试从他的专业Asp.Net MVC框架书(伟大的书,顺便说一句)中实现Steven Sanderson的WinsorControllerFactory,我陷入了一个问题。我不确定为了制定回复你还需要知道什么,但我非常感谢你提供帮助。谢谢!
以下是代码:
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()));
}
错误消息 没有用于支持服务的组件****。发现了NewsArticleController 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中的起源位置的更多信息。
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: }
答案 0 :(得分:2)
S#arpArchitecture项目可以很好地使用Windsor作为DI框架(用于控制器或其他任何东西),很少或根本不需要添加web.config部分 - http://code.google.com/p/sharp-architecture/
代码示例:
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(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));
}
答案 1 :(得分:2)
MvcContrib提供了一些在Windsor中注册控制器的扩展方法,例如:
windsorContainer.RegisterControllers(Assembly.GetExecutingAssembly());
Mannish的代码使用相同的扩展方法。
答案 2 :(得分:1)
不要忘记WindsorControllerFactory上的ReleaseController函数,否则你的工作进程将耗尽内存,因为控制器没有被释放。