我使用ASP.NET MVC(1.0)和StructureMap(2.5.3),我在做其中的dll與控制器都到一個文件夾中拿起一個插件功能。我註冊SM控制器(我可以把它撿起來算賬,所以我知道它在那裏)插件控制器,StructureMap和ASP.NET MVC
foreach (string file in path)
{
var assy = System.Reflection.Assembly.LoadFile(file);
Scan(x =>{
x.Assembly(assy);
x.AddAllTypesOf<IController>();
});
}
我的問題是我DefaultControllerFactory的覆蓋的GetControllerInstance方法。每次我發送的東西都不是有效的控制器(有效的,因爲它是Web項目的一部分),我得到輸入類型參數爲空。
我試着爲它建立特定的路由。
我已經做了Castle.Windsor一個測試,那裏是沒有問題的。
任何人都可以指向正確的方向嗎?我會很感激。
[編輯]
下面是代碼:
- >控制器工廠溫莎
public WindsorControllerFactory()
{
container = new WindsorContainer(new XmlInterpreter(
new ConfigResource("castle")));
// Register all the controller types as transient
// This is for the regular controllers
var controllerTypes =
from t in
Assembly.GetExecutingAssembly().GetTypes()
where typeof(IController).IsAssignableFrom(t)
select t;
foreach (Type t in controllerTypes)
{
container.AddComponentLifeStyle(t.FullName, t,
LifestyleType.Transient);
}
/* Now the plugin controllers */
foreach (string file in Plugins())
{
var assy = System.Reflection.Assembly.LoadFile(file);
var pluginContr =
from t in assy.GetTypes()
where typeof(IController).IsAssignableFrom(t)
select t;
foreach (Type t in pluginContr)
{
AddToPlugins(t);
/* This is the only thing I do, with regards to Windsor,
for the plugin Controllers */
container.AddComponentLifeStyle(t.FullName, t,
LifestyleType.Transient);
}
}
}
- > StructureMap;添加控制器:
public class PluginRegistry : Registry
{
public PluginRegistry()
{
foreach (string file in Plugins()) // Plugins return string[] of assemblies in the plugin folder
{
var assy = System.Reflection.Assembly.LoadFile(file);
Scan(x =>
{
x.Assembly(assy);
//x.AddAllTypesOf<IController>().
// NameBy(type => type.Name.Replace("Controller", ""));
x.AddAllTypesOf<IController>();
});
}
}
}
- >控制器工廠SM版本 不是真的這樣做了,因爲我在前面的步驟與SM註冊控制器
public SMControllerFactory()
: base()
{
foreach (string file in Plugins())
{
var assy = System.Reflection.Assembly.LoadFile(file);
var pluginContr =
from t in assy.GetTypes()
where typeof(IController).IsAssignableFrom(t)
select t;
foreach (Type t in pluginContr)
{
AddPlugin();
}
}
}
你是不是想掃描所有組件在StructureMap註冊表,並在控制器的工廠?這就是它的樣子,但它確實沒有意義。您的StructureMap註冊表應該執行所有掃描以在容器中註冊所有控制器類型。控制器工廠然後向容器詢問所請求的控制器類型的實例。 – 2010-01-30 20:41:36