[英]Using Common.Logging with Asp.net MVC and Castle Windsor
[英]Need help with Castle.Windsor as an Asp.Net MVC ControllerFactory please
我正在嘗試從他的親Asp.Net MVC框架書(很棒的書,btw)中實現史蒂文·桑德森的WinsorControllerFactory,而我正陷入一個問題。 我不確定您還需要知道什么才能提出回應,但對此我將不勝感激。 謝謝!
這是代碼:
溫莎控制器工廠
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配置
<castle>
<components>
<component id="MenuRepository"
service="****.IMenuRepository, ****.Model"
type="****.FakeMenuRepository, ****.Model">
</component>
<component id="NewsRepository"
service="****.INewsRepository, ****.Model"
type="****.FakeNewsRepository, ****.Model">
</component>
</components>
</castle>
新聞ArticleController
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: }
S#arpArchitecture項目可以將Windsor用作DI框架(用於控制器或其他任何東西),幾乎不需要添加web.config部分-http: //code.google.com/p/sharp-建築/
代碼示例:
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));
}
MvcContrib提供了一些擴展方法,用於在Windsor中注冊控制器,例如:
windsorContainer.RegisterControllers(Assembly.GetExecutingAssembly());
Mannish的代碼使用相同的擴展方法。
不要忘記WindsorControllerFactory上的ReleaseController函數,否則您的工作進程將耗盡內存,因為未釋放控制器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.