外部控制器和城堡 [英] External Controllers and Castle

查看:49
本文介绍了外部控制器和城堡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已修复:如果其他一些Joe Schmo需要它,我会保留它。

FIXED: I'm leaving this in case some other Joe Schmo needs it.

在控制器工厂中,您需要注册控制器以便可以找到它当被叫。 container.Kernel.AddComponent( ExternalResources,typeof(InteSoft.Web.ExternalResourceLoader.ExternalResourceController),LifestyleType.Transient); 这样做:

In the controller factory you need to register the controller so that it can be found when called. container.Kernel.AddComponent("ExternalResources", typeof(InteSoft.Web.ExternalResourceLoader.ExternalResourceController), LifestyleType.Transient); Do it like so:

// Instantiate a container, taking configuration from web.config
        container = new WindsorContainer(
        new XmlInterpreter(new ConfigResource("castle"))
        );
        // Also register all the controller types as transient
        var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                              where typeof(IController).IsAssignableFrom(t)
                              select t;

        foreach (Type t in controllerTypes)
            container.AddComponentWithLifestyle(t.FullName, t,
            LifestyleType.Transient);

        // Register controllers in external assemblies
        container.Kernel.AddComponent("ExternalResources", typeof(InteSoft.Web.ExternalResourceLoader.ExternalResourceController), LifestyleType.Transient);

我正在使用 MVC资源加载器可压缩并缩小我的CSS和JS。我还使用WindsorControllerFactory进行依赖项注入。 MVC REsource加载程序使用位于单独程序集中的InteSoft.Web.ExternalResourceLoader命名空间中的控制器。

I am using the MVC Resource loader to compress and minify my CSS and JS. I am also using the WindsorControllerFactory for dependency injection. MVC REsource loader uses a controller that is located in the InteSoft.Web.ExternalResourceLoader namespace which is in a seperate assembly.

问题似乎是Castle无法找到(并解决)该控制器,因为它在另一个程序集中。我是DI和Castle的新手,所以我不确定从哪里开始。

The problem seems to be that Castle can't find (and resolve) that controller because it is in a different assembly. I'm pretty new to DI and Castle so I'm not sure where to even start.

城堡配置文件

<component id="MVCResourceLoader"
             service="System.Web.Mvc.ITempDataProvider, System.Web.Mvc"
             type="InteSoft.Web.ExternalResourceLoader.NullTempDataProvider, InteSoft.Web.ExternalResourceLoader"
             lifestyle="PerWebRequest">           
</component>

温莎控制器工厂

public class WindsorControllerFactory : DefaultControllerFactory
{
    WindsorContainer container;
    // The constructor:
    // 1. Sets up a new IoC container
    // 2. Registers all components specified in web.config
    // 3. Registers all controller types as components
    public WindsorControllerFactory()
    {
        // Instantiate a container, taking configuration from web.config
        container = new WindsorContainer(
        new XmlInterpreter(new ConfigResource("castle"))
        );
        // Also register all the controller types as transient
        var controllerTypes = from t in Assembly.GetExecutingAssembly().GetTypes()
                              where typeof(IController).IsAssignableFrom(t)
                              select t;

        foreach (Type t in controllerTypes)
            container.AddComponentWithLifestyle(t.FullName, t,
            LifestyleType.Transient);

    }
    // Constructs the controller instance needed to service each request
    protected override IController GetControllerInstance(Type controllerType)
    {
        return (IController)container.Resolve(controllerType);
    }
}

错误页面

    Server Error in '/' Application.
The type name InteSoft.Web.ExternalResourceLoader.NullTempDataProvider, InteSoft.Web.ExternalResourceLoader could not be located
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: System.Configuration.ConfigurationErrorsException: The type name InteSoft.Web.ExternalResourceLoader.NullTempDataProvider, InteSoft.Web.ExternalResourceLoader could not be located

Source Error:

Line 23:         {
Line 24:             // Instantiate a container, taking configuration from web.config
Line 25:             container = new WindsorContainer(
Line 26:             new XmlInterpreter(new ConfigResource("castle"))
Line 27:             );


Source File: C:\Projects\CaseLogger Pro\CaseLogger.Website\WindsorControllerFactory.cs    Line: 25

Stack Trace:

[ConfigurationErrorsException: The type name InteSoft.Web.ExternalResourceLoader.NullTempDataProvider, InteSoft.Web.ExternalResourceLoader could not be located]
   Castle.Windsor.Installer.DefaultComponentInstaller.ObtainType(String typeName) +81
   Castle.Windsor.Installer.DefaultComponentInstaller.SetUpComponents(IConfiguration[] configurations, IWindsorContainer container) +132
   Castle.Windsor.Installer.DefaultComponentInstaller.SetUp(IWindsorContainer container, IConfigurationStore store) +66
   Castle.Windsor.WindsorContainer.RunInstaller() +35
   Castle.Windsor.WindsorContainer..ctor(IConfigurationInterpreter interpreter) +60
   CaseLogger.Website.WindsorControllerFactory..ctor() in C:\Projects\CaseLogger Pro\CaseLogger.Website\WindsorControllerFactory.cs:25
   CaseLogger.MvcApplication.Application_Start() in C:\Projects\CaseLogger Pro\CaseLogger.Website\Global.asax.cs:50


Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082 

请告诉我是否需要提供更多信息调试信息。

更新
如果我直接访问该网址,它将返回压缩文件,例如 http:// localhost:3826 / Shared / ExternalResource?name = MinScript& version = 20080811& display = Show

推荐答案

我认为您将无法获得服务通过调用 Assembly.GetExecutingAssembly()。GetTypes()在外部组装中。

I don't think you will be able to get services in external assebmly by calling Assembly.GetExecutingAssembly().GetTypes().

尝试使用 Assembly.LoadFrom( Assembly_Name),然后向 ExternalResources 注册该组件。

Try to use Assembly.LoadFrom("Assembly_Name") and then register the component with "ExternalResources".

这篇关于外部控制器和城堡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆