本文介绍了等效于非Web应用程序的BuildManager.GetReferencedAssemblies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AppDomain.GetAssemblies()BuildManager.GetReferencedAssemblies()相比( System.Web.Compilation.BuildManager )似乎是获取ASP.NET应用程序在运行时引用的程序集的更可靠的方法,因为 AppDomain.GetAssemblies()仅获取已将 加载到程序集的执行上下文中的程序集此应用程序域".

Compared to AppDomain.GetAssemblies(), BuildManager.GetReferencedAssemblies() (System.Web.Compilation.BuildManager) seems a more reliable way to get the assemblies that are referenced by an ASP.NET application at runtime, since AppDomain.GetAssemblies() only gets "the assemblies that have already been loaded into the execution context of this application domain".

遍历所有程序集是在DI容器中启动应用程序时(尤其是在应用程序启动时)动态注册类型的重要工具,尤其是在尚未加载其他程序集(不需要)的情况下,其组成也很重要. root是第一个需要它们的人.因此,拥有一种可靠的方法来获取应用程序引用的程序集非常重要.

Iterating through all assemblies is an important tool for dynamically registering types at application start-up in your DI container and especially during application start-up, chances are that other assemblies are not loaded (where not needed) yet, and the composition root is the first one that needs them. It is therefore very important to have a reliable method to get the application's referenced assemblies.

尽管BuildManager.GetReferencedAssemblies()是ASP.NET应用程序的可靠方法,但我想知道:其他类型的应用程序可以使用哪些替代方法 ,例如桌面应用程序,Windows服务和自托管WCF服务?

Although BuildManager.GetReferencedAssemblies() is a reliable method for ASP.NET applications, I'm wondering: what alternatives are available for other types of applications, such as desktop applications, windows services and self-hosted WCF services?

推荐答案

我目前看到的唯一方法是手动预取所有引用的程序集,就像BuildManager在幕后所做的一样:

The only way I currently see is pre-fetching all referenced assemblies manually, just as the BuildManager does under the covers:

var assemblies =
    from file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory)
    where Path.GetExtension(file) == ".dll"
    select Assembly.LoadFrom(file);

这篇关于等效于非Web应用程序的BuildManager.GetReferencedAssemblies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 19:20