固定:我要离开这个以防其他Joe 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
如果我需要提供更多调试信息,请与我们联系。
更新 如果我直接访问该URL,则返回压缩文件,例如http://localhost:3826/Shared/ExternalResource?name=MinScript&version=20080811&display=Show
答案 0 :(得分: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>
答案 1 :(得分:0)
我认为您无法通过调用Assembly.GetExecutingAssembly().GetTypes()
来获取外部程序集中的服务。
尝试使用Assembly.LoadFrom("Assembly_Name")
,然后使用"ExternalResources"
注册该组件。
答案 2 :(得分:0)
Fyi,这是我使用Castle的ControllerFactory:
Why is Castle Windsor trying to resolve my 'Content' and 'Scripts' folder as a controller?
请注意,我使用的是Assembly.GetCallingAssembly()。GetTypes(),因为我的自定义ControllerFactory位于一个单独的程序集中。如果要将它放在主mvc项目中,请将其更改为getExecutingAssembly()。