c# - 在 MVC 中设置 CaSTLe Windsor

标签 c# asp.net-mvc castle-windsor

我是第一次尝试设置 CaSTLe Windsor,但遇到了一些问题。我的解决方案中有三个项目:

  • 域名
  • DAL
  • 网络

服务位于 DAL 中。它们都继承自IService。 (UserService 实现IUserServiceIUserService 实现IService)。 Web 应用程序是一个 MVC 5 应用程序。所有 Controller 都继承自 BaseController

我用了this发帖帮助我设置 Windsor,但我一直收到异常:

An exception of type 'Castle.MicroKernel.ComponentNotFoundException' occurred in Castle.Windsor.dll but was not handled in user code

Additional information: No component for supporting the service Solution.Web.Controllers.HomeController was found

奇怪的是 Controller 的路径是正确的。

下面是我的配置代码:

public class WindsorControllerFactory : DefaultControllerFactory
{
  private readonly IKernel kernel;

  public WindsorControllerFactory(IKernel kernel)
  {
    this.kernel = kernel;
  }

  public override void ReleaseController(IController controller)
  {
    kernel.ReleaseComponent(controller);
  }

  protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
  {
    if (controllerType == null)
    {
      throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
    }
    return (IController)kernel.Resolve(controllerType);
  }
}

public class ControllersInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
          Classes.FromThisAssembly()
          .BasedOn(typeof(BaseController))
          .LifestyleTransient());
    }
}

public class ServiceInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Types.FromAssemblyContaining(typeof(IService).GetType())
            .BasedOn<IService>().WithService.FromInterface()
            .LifestyleTransient()
        );
    }
}

在 Global.asax 中:

public class MvcApplication : System.Web.HttpApplication
{

    private static IWindsorContainer container;

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        // Setup Castle.Windsor IOC
        MvcApplication.BootstrapContainer();
    }
    protected void Application_End()
    {
        container.Dispose();
    }

    private static void BootstrapContainer()
    {
        container = new WindsorContainer().Install(FromAssembly.This());
        container.Install(FromAssembly.Containing(typeof(IService).GetType()));
        var controllerFactory = new WindsorControllerFactory(container.Kernel);
        ControllerBuilder.Current.SetControllerFactory(controllerFactory);
    }
}

非常感谢任何正确方向的帮助或指导!

最佳答案

我假设它在这里爆炸:

return (IController)kernel.Resolve(controllerType);

你在这里要求城堡用英语做的是“给我实现由 controllerType 定义的服务的组件”。

问题出在您注册 Controller 时。

container.Register(
      Types.FromThisAssembly()
      .BasedOn(typeof(BaseController))
      .WithServices(typeof(BaseController))
      .LifestyleTransient());

在此 block 中,您将告诉城堡注册所有实现 BaseController 的类型,并且它们公开的服务也是 BaseController。

所以 caSTLe 正在寻找一个满足服务 HomeController 的组件,但找不到任何东西,因为你得到的唯一服务是 BaseController。

长话短说,如果你删除

.WithServices(typeof(BaseController))

CaSTLe 会假定您的每个 Controller 都是一项服务,然后您可以请求以您想要的方式实现 Controller 的组件。

作为单独的注释,为了清楚起见,我将 Types.FromThisAssembly() 更改为 Classes.FromThisAssembly(),因为您只查找类,而不是类型将扫描的结构/类/接口(interface)等。

关于c# - 在 MVC 中设置 CaSTLe Windsor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19743154/

相关文章:

c# - 确定窗口是否处于帮助模式

c# - WP7 游戏的 XNA : DrawUserIndexedPrimitives

asp.net-mvc - dropzonejs 多文件上传无法按预期工作

nhibernate - 在共享主机上使用 CaSTLe Windsor 和 NHibernate 设施

c# - 如何访问使用 groupBy 的 LINQ 查询中的元素?

c# - CommandType为StoredProcedure时如何使用IDbCommandInterceptor修改SQL

c# - 为什么 WindsorDependencyResolver 无法解析?

c# - 使用 CaSTLe Windsor IoC 容器时添加服务初始化方法

asp.net-mvc - Visual Studio 不将 html/javascript 更新到服务器/浏览器

asp.net-mvc - Azure 计划长时间运行的任务