2009-07-22 25 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"註冊該組件。