C# 从另一个程序集引导Windows窗体项目
我在将洋葱架构与UI层相结合时遇到了一个障碍。问题是我的配置方法从未被命中。IoC设置在依赖项解析程序集中进行:C# 从另一个程序集引导Windows窗体项目,c#,winforms,onion-architecture,webactivator,C#,Winforms,Onion Architecture,Webactivator,我在将洋葱架构与UI层相结合时遇到了一个障碍。问题是我的配置方法从未被命中。IoC设置在依赖项解析程序集中进行: Project.Core Project.Infrastructure Project.UI <- Startup project Project.DependencyResolution <- IoC configuration OutputTo的OutputTargets.txt: ..\Project.UI\bin
Project.Core
Project.Infrastructure
Project.UI <- Startup project
Project.DependencyResolution <- IoC configuration
OutputTo的OutputTargets.txt:
..\Project.UI\bin
在Project.UI中:
static class Program
{
static void Main() {
WebActivatorEx.ActivationManager.RunPreStartMethods();
Application.Run(...);
}
}
OutputTo将DependencyResolution的
DLL文件正确复制到Ui的
bin,但IocConfig.RegisterDependencies
从不运行
那么,如何从自己的程序集(其中Windows窗体项目是启动项目)设置IoC呢?刚刚用WebActivatex 2.0.0.5(NuGet中最新版本)测试了这一点。很好。通过在
RegisterDependencies
中将某些内容打印到控制台进行检查
在任何情况下,它都与WinForms应用程序无关(可能是一个控制台应用程序,它仍然可以工作)
我现在唯一想到的是,您的UI程序集不在其他程序集(包括WebActivatex)的旁边。我检查了它的源代码,它依赖于它的存在,因为它在那里查找所有DLL。你能确定这些组件在它们应该在的地方吗
此外,WebActivatex在其源代码中包含以下内容:
try
{
return assembly.GetCustomAttributes(
typeof(T),
inherit: false).OfType<T>();
}
catch
{
// In some very odd (and not well understood) cases, GetCustomAttributes throws. Just ignore it.
// See https://github.com/davidebbo/WebActivator/issues/12 for details
return Enumerable.Empty<T>();
}
试试看
{
返回assembly.GetCustomAttributes(
类型(T),
继承:false);
}
抓住
{
//在一些非常奇怪的情况下(也不是很好理解),GetCustomAttributes抛出。忽略它。
//看https://github.com/davidebbo/WebActivator/issues/12 详情
返回可枚举的.Empty();
}
因此,如果您没有及时找到原因,我建议您获取WebActivatex源代码,并使用它来调试行为。您还可以看到它在
ActivationManager.RunPreStartMethods
中加载了哪些程序集(它实际上是私有的静态assemblies
属性)。在同一个解决方案中,所有程序集都紧挨着彼此。我将继续并提取源代码并进行调试。谢谢你的回复。@Smith.h.Neil:他们在磁盘上也必须相邻,而不仅仅是在解决方案中。是的,你是对的。OutputTo正在将DependencyResolution dll复制到Project.UI的bin中。将其复制到bin/debug,而是将它们放在一起,这样做了。再次感谢。
try
{
return assembly.GetCustomAttributes(
typeof(T),
inherit: false).OfType<T>();
}
catch
{
// In some very odd (and not well understood) cases, GetCustomAttributes throws. Just ignore it.
// See https://github.com/davidebbo/WebActivator/issues/12 for details
return Enumerable.Empty<T>();
}