使用程序集名称而不是路径加载不在 Web 应用程序的 GAC 或 bin 目录中的程序集
我有一个 dll,位于目录 xyz 中。有没有办法在不实际指定路径的情况下加载该 dll?
我知道我可以通过指定 dll 的路径来加载它, 但后来我遇到了一个非常烦人的问题,它迫使我将该 dll 复制到我的应用程序 bin 目录并使用其名称加载它。
我希望 bin 目录中没有该 dll。那么有没有一种方法可以使用实际上不在 GAC 或 bin 目录中的名称来加载程序集?
我可以做些什么来让我的 Web 应用程序扫描程序集的其他目录吗?
I have a dll which is in directory xyz. Is there a way to load that dll without actually specifying the path?
I know I could load it simply by specifying the path to the dll, but then I bumped into a pretty annoying issue that forces me to copy that dll to my applications bin directory and load it using its name.
I would prefer though to not have that dll in the bin directory. So is there a way to load an assembly using its name which is actually not located in the GAC or bin directory?
Is there something I can do to have my web application scan additional directories for an assembly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 app.config 中指定在何处查找程序集。
Probing
元素。You can specify in you app.config where to look for assemblies.
Probing
Element.您可以在您的 .网络配置。
但是,我怀疑这是否能解决您在另一个问题中的问题。
You can use the
probing
element in your web.config.I doubt however, that this resolves your problem in the other question.
作为基于配置的答案的替代方法,您可以注册 AppDomain.AssemblyResolve 事件。
这使您有机会在程序集解析失败时运行一些代码,因此您可以尝试在运行时纠正问题(例如,如果您感觉很聪明,可以对 dll 运行递归目录搜索)。
这可能比使用配置更灵活,但代价是一些额外的复杂性。
As an alternative approach to the config based answers, you can register for the AppDomain.AssemblyResolve event.
This gives you a chance to run some code when assembly resolution fails, so you can try to rectify the problem at runtime (for example you could run a recursive directory search for the dll if you're feeling saucy).
This could be more flexible than using configuration, at the expense of some extra complexity.
您可能想要研究程序集探测
,并在此处检查:运行时如何定位程序集:http://msdn.microsoft.com/en-us/library/15hyw9x3(v=VS.100).aspx
You might want to look into assembly probing
and also check here: How the runtime locates assemblies: http://msdn.microsoft.com/en-us/library/15hyw9x3(v=VS.100).aspx