.net 外部控制器和城堡

.net 外部控制器和城堡,.net,dependency-injection,castle-windsor,.net,Dependency Injection,Castle Windsor,修正:我留下这个以防其他乔·施莫需要 在控制器工厂中,您需要注册控制器,以便在调用时可以找到它container.Kernel.AddComponent(“ExternalResources”,typeof(InteSoft.Web.ExternalResourceLoader.ExternalResourceController),lifetyleType.Transient)这样做: // Instantiate a container, taking configuration from

修正:我留下这个以防其他乔·施莫需要

在控制器工厂中,您需要注册控制器,以便在调用时可以找到它container.Kernel.AddComponent(“ExternalResources”,typeof(InteSoft.Web.ExternalResourceLoader.ExternalResourceController),lifetyleType.Transient)这样做:

// 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);
我正在使用压缩和缩小我的CSS和JS。我还使用WindsorController工厂进行依赖注入。MVC资源加载器使用位于InteSoft.Web.ExternalResourceLoader命名空间中的控制器,该命名空间位于单独的程序集中

问题似乎是Castle无法找到(并解决)该控制器,因为它位于不同的程序集中。我对DI和Castle很陌生,所以我甚至不知道从哪里开始

城堡配置文件

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

    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 
如果我需要提供更多调试信息,请告诉我。

更新
如果我直接访问url,它将返回压缩文件,例如

,很可能NullTempDataProvider所在的程序集是InteSoft.Web,而不是InteSoft.Web.ExternalResourceLoader。在这种情况下,注册应为:

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

我认为通过调用
Assembly.getExecutionGassembly().GetTypes()
无法在外部Assembly中获取服务


尝试使用
Assembly.LoadFrom(“Assembly\u Name”)
,然后将组件注册到
“ExternalResources”

供参考,以下是我使用Castle的ControllerFactory:

请注意,我使用的是Assembly.GetCallingAssembly().GetTypes(),因为我的自定义ControllerFactory位于单独的程序集中。如果要将其放置在主mvc项目中,请将其更改为GetExecutionGassembly()

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