gpt4 book ai didi

c# - 从所有程序集中在 CaSTLe Windsor 容器中以编程方式注册所有 Controller

转载 作者:行者123 更新时间:2023-11-30 13:05:58 25 4
gpt4 key购买 nike

我用这个代码...

        container.Register(
AllTypes
.FromAssembly(Assembly.Load("MyNamespace.Dashboard"))
.BasedOn<IController>()
.Configure(component => component.LifeStyle.Transient
.Named(ControllerNameFromType(component.Implementation)))
);

... 在容器中注册我的 Controller ,但我希望能够注册所有程序集中的所有 Controller ,以使事情更具可插拔性。我认为下面的代码应该有效,但它不起作用?

        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)))
);
}

最佳答案

检查程序集以查看是否所有程序集都已加载。

如果没有,做这样的事情:

        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());
}
}

关于c# - 从所有程序集中在 CaSTLe Windsor 容器中以编程方式注册所有 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1238727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com