我使用此代码......
container.Register(
AllTypes
.FromAssembly(Assembly.Load("MyNamespace.Dashboard"))
.BasedOn<IController>()
.Configure(component => component.LifeStyle.Transient
.Named(ControllerNameFromType(component.Implementation)))
);
...在容器中注册我的控制器但我希望能够注册所有组件中的所有控制器以使事情更加可插拔。我认为下面的代码应该可以工作,但是它可以吗?
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies) {
container.Register(
AllTypes
.FromAssembly(assembly)
.BasedOn<IController>()
.Configure(component => component.LifeStyle.Transient
.Named(ControllerNameFromType(component.Implementation)))
);
}
答案 0 :(得分:0)
检查程序集以查看是否已加载所有程序集。
如果不这样做,请执行以下操作:
foreach (string dllPath in Directory.GetFiles(directoryPath, "*.dll"))
{
try
{
Assembly a = Assembly.ReflectionOnlyLoadFrom(dllPath);
if (Matches(a.FullName) && !loadedAssemblyNames.Contains(a.FullName))
{
App.Load(a.FullName);
}
}
catch (BadImageFormatException ex)
{
Trace.TraceError(ex.ToString());
}
}