为什么运行时不会自动解决我的程序集依赖关系?
我有一些 C++ 代码,它是基于 MMC 的应用程序的管理单元。此管理单元通过 COM 包装器 (AssemblyA) 使用 .net 2.0 dll。 AssemblyA 与启动 MMC 会话的应用程序位于同一目录中。 AssemblyA 使用一些其他 .net dll (OtherAssemblies),由于我无法控制的原因,它们不能与 AssemblyA 位于同一目录中。它还允许动态加载某些组件(从 AssemblyB),并在第三个目录中搜索这些组件。 AssemblyB 中的动态组件引用 AssemblyA,因为它们扩展了其中的基类。
我的问题是,当我尝试加载动态组件时,它无法解析对 AssemblyA 的依赖关系,并且我的 AssemblyResolve
处理程序被触发(我用来解析 OtherAssemblies)。当我在
AssemblyResolve
处理程序中查询 Assembly.GetExecutingAssembly ()
时,该程序集就是我要解析的程序集。
这种行为对我来说似乎有点奇怪,因为我希望 .NET 运行时首先在加载的程序集中查找依赖项,然后在我正在加载的程序集本地查找依赖项,然后在应用程序目录中查找。其中第一个和第三个应该包含我尝试加载的程序集。
我修改了我的 AssemblyResolve 方法,以便它搜索其他位置的依赖项,以便它可以工作,例如当前的应用程序目录,但如果我可以帮助的话,我真的不想这样做。
这种行为是预期的吗?是因为它是一个 MMC 应用程序还是因为它是从 C++ 调用的 COM 启动的?我是个笨蛋吗?
I have some c++ code that is a snap-in for a MMC based application. This snap in uses a .net 2.0 dll via a COM wrapper (AssemblyA). AssemblyA lives in the same directory as the app that launches the MMC session. AssemblyA uses some other .net dlls (OtherAssemblies) which, for reasons beyond my control cannot live in the same directory as AssemblyA. It also allows some components to be dynamically loaded (from AssemblyB), and it searches for these in a third directory. The dynamic components from AssemblyB reference AssemblyA as they extend a base class in there.
My problem is that when the I attempt to load the dynamic component it is unable to resolve the dependency to AssemblyA, and my AssemblyResolve
handler gets fired (which I was using to resolve OtherAssemblies
). When I query Assembly.GetExecutingAssembly ()
in the AssemblyResolve
handler, the assembly is the assembly I'm trying to resolve.
This behaviour seems a little strange to me as I would have expected the .NET runtime to look for dependencies in the loaded assemblies first, then local to the Assembly I'm loading, then in the app directory. The first and third of these should contain the assembly I'm trying to load.
I have modified my AssemblyResolve method so that it searches for the dependencies in other locations so it works, like the current app directory, but I don't really want to do this if I can help it.
Is this behaviour expected? Is it due to it being an MMC app or due to the fact that it is launched from COM called from C++? Am I being a dunce?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两者都是。事实上,您正在运行 MMC,这使得 .NET 很难解析程序集,它不会在 c:\windows\system32 中找到任何程序集。您无法使用 MMC 的 .config 文件合理地解决此问题,它会与任何未来的插件产生冲突。
COM 也没有帮助,放置 COM DLL 的目录不会以任何方式影响探测路径。您已经找到了一种解决此问题的方法:AssemblyResolve。或者您可以将所有内容都放入 GAC 中。
或者您可以避免使用 COM 并直接使用 Microsoft.ManagementConsole 命名空间。
It is both. The fact that you're running MMC makes it hard for .NET to resolve assemblies, it isn't going to find any in c:\windows\system32. You can't reasonably solve this with a .config file for MMC, it is going to muck with any future plug-ins.
COM doesn't help either, the directory in which you placed the COM DLL does not in any way affect the probing path. You already found one way to solve this, AssemblyResolve. Or you could put everything in the GAC.
Or you could avoid using COM and write MMC plug-ins directly with the Microsoft.ManagementConsole namespace.