不能在ASP.NET MVC中添加控制器到Castle.Windsor [英] Cannot add Controllers to Castle.Windsor in ASP.NET MVC
问题描述
,我有三个组件。
网页(ASP.NET MVC 3.0 RC1)
Web (ASP.NET MVC 3.0 RC1)
模式
持久性(功能NHibernate,Castle.Windsor)
Persistence (Fluent NHibernate, Castle.Windsor)
这是我的ControllerInstaller。
This is my ControllerInstaller.
using System;
using System.Web.Mvc;
using Castle;
using Castle.Windsor;
using Castle.MicroKernel;
using Castle.MicroKernel.SubSystems;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
namespace Persistence.Installers
{
public class ControllerInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
AllTypes
.FromAssembly(System.Reflection.Assembly.GetExecutingAssembly())
.BasedOn<IController>()
.Configure(c => c.Named(
c.Implementation.Name.ToLowerInvariant()).LifeStyle.Transient));
}
}
}
这是我的ControllerFactory ...
This is my ControllerFactory...
using System;
using System.Web;
using System.Web.Mvc;
namespace Persistence.Containers
{
/// <summary>
/// Utilize Castle.Windsor to provide Dependency Injection for the Controller Factory
/// </summary>
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly Castle.Windsor.IWindsorContainer container;
public WindsorControllerFactory()
{
container = WindsorContainerFactory.Current();
}
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return (IController)container.Resolve(controllerType);
}
}
}
这是我的的Application_Start
在的Global.asax
文件..
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
// Register the Windsor Container
ControllerBuilder.Current
.SetControllerFactory(typeof(Persistence.Containers.WindsorControllerFactory));
}
我收到错误
有关配套服务Project.Web.Controllers.HomeController的成分,不被发现。
No component for supporting the service Project.Web.Controllers.HomeController was found
在 GetControllerInstance
。
所以,我真的不知道我在做什么错,为什么我不能得到控制器注册。
So , I'm not really sure what I am doing wrong, and why I cannot get the Controllers registered.
推荐答案
您温莎城堡设置code都属于你的Web项目。这是无关的持久性。
Your Castle Windsor setup code all belongs in your Web project. It is nothing to do with Persistence.
这是造成因为你的 ControllerInstaller
正在尝试注册在持久组装,而不是与下面code网络组件控制器的问题:
This is causing the problem because your ControllerInstaller
is trying to register the controllers in the Persistence assembly rather than the Web assembly with the following code:
System.Reflection.Assembly.GetExecutingAssembly()
。
所以移动的IoC code到Web项目,您会发现您的控制器。
So move the IoC code to the Web project and it will find your controllers.
这篇关于不能在ASP.NET MVC中添加控制器到Castle.Windsor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!