2009-07-22 22 views
0

固定:我要离开这个以防其他一些乔Schmo需要它。外部控制器和城堡

在控制器工厂中,您需要注册控制器,以便在调用时可以找到控制器。 container.Kernel.AddComponent(“ExternalResources”,typeof(InteSoft.Web.ExternalResourceLoader.ExternalResourceController),LifestyleType.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); 

我使用MVC Resource loader压缩,然后再缩小我的CSS和JS。我也使用WindsorControllerFactory进行依赖注入。 MVC REsource加载器使用位于独立程序集中的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> 

温莎器厂

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 

请让我知道如果我需要提供更多的调试信息。

UPDATE 如果我访问的URL直接返回压缩文件例如http://localhost:3826/Shared/ExternalResource?name=MinScript&version=20080811&display=Show

回答

0

最有可能是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> 
0

我不认为你可以通过调用Assembly.GetExecutingAssembly().GetTypes()获得外部assebmly服务。

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