简体   繁体   English

引用的程序集未找到 - 如何获取解决方案中包含的所有DLL

[英]Referenced Assembly Not Found - How to get all DLLs included in solution

I'm running a WCF application CoreApplication whose VS project has a reference to AncillaryProject . 我正在运行一个WCF应用程序CoreApplication其VS项目引用了AncillaryProject CoreApplication uses a class Provider from AncillaryProject ; CoreApplication使用AncillaryProject的类Provider ; however, it is never explicitly referenced - it's invoked via Reflection. 但是,它永远不会被明确引用 - 它是通过Reflection调用的。

My problem is that sometimes CoreApplication fails to find Provider because AncillaryProject does not come up in the call to GetAssemblies() . 我的问题是, 有时 CoreApplication无法找到Provider因为AncillaryProject没有出现在对GetAssemblies()的调用中。 Sometimes it works fine, but sometimes (I'm guessing it may be after a JIT) it fails. 有时它工作正常,但有时(我猜它可能是在JIT之后)它失败了。

Here's my original code: 这是我的原始代码:

var providers = from d in AppDomain.CurrentDomain.GetAssemblies()
                from c in d.GetTypes()
                where typeof(BaseProvider).IsAssignableFrom(c)
                select c;

After looking at this question , I tried using GetReferencedAssemblies() : 看了这个问题后 ,我尝试使用GetReferencedAssemblies()

var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
    allAssemblies = allAssemblies.Union(
                          a.GetReferencedAssemblies()
                           .Select(b => System.Reflection.Assembly.Load(b)));
}
var providers = from d in allAssemblies
                from c in d.GetTypes()
                where typeof(BaseProvider).IsAssignableFrom(c)
                select c;

I realize that the question I referenced solves the problem through dynamically loading all dll files in the bin directory, but that doesn't sound particularly good to me. 我意识到我引用的问题通过动态加载bin目录中的所有dll文件来解决问题,但这对我来说听起来不是特别好。 Is there a better way to do this , or is .NET simply not loading the other Assemblies in at all? 有没有更好的方法来做到这一点 ,或者.NET根本不加载其他程序集? How does this work under the hood , and is there anything I can do about it? 这是如何工作的 ,我能做些什么呢?

According to Microsoft documentation AppDomain.CurrentDomain.GetAssemblies() gets the assemblies that have been loaded into the execution context of this application domain. 根据Microsoft文档, AppDomain.CurrentDomain.GetAssemblies()获取已加载到此应用程序域的执行上下文中的程序集。 About AppDomain.CurrentDomain.GetAssemblies() 关于AppDomain.CurrentDomain.GetAssemblies()

It seems that you need to change strategy of loading the assemblies you need from using the appdomain to looking for dlls in your applications folder. 您似乎需要更改从使用appdomain加载所需程序集的策略,以便在应用程序文件夹中查找dll。

I found a discussion on a similar problem here 我在这里找到了关于类似问题的讨论

You should download the .NET Development SDK and start up FuslogVw.exe (fusion log viewer). 您应该下载.NET Development SDK并启动FuslogVw.exe (融合日志查看器)。 It will report on CLR Application trying to resolve .NET dependencies. 它将报告尝试解析.NET依赖关系的CLR应用程序。 It will show you were it is looking and how it evaluates the candidates located at those places. 它会告诉你它是在寻找它以及它如何评估那些地方的候选人。

You can handle the AssemblyResolve event and load AncillaryProject.dll in that event handler 您可以处理AssemblyResolve事件并在该事件处理程序中加载AncillaryProject.dll

http://msdn.microsoft.com/en-us/library/ff527268.aspx http://msdn.microsoft.com/en-us/library/ff527268.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM