英文:
Dynamically loaded .NET core library with .NET framework dependencies doesn't load the successfully, but in project reference does
问题
我正在从OEM的网站加载一个.NET Framework DLL到一个.NET 6.0包装DLL中。
然后我将这个包装器加载到我的.NET 6.0 Winforms应用程序中:
- 应用程序(.NET 6.0)使用
- DLL(.NET 6.0)使用
- DLL(.NET Framework)
我遇到了一个错误:
无法加载文件或程序集'XXX,Version=1.0.9.0, Culture=neutral, PublicKeyToken=null'。系统找不到指定的文件。
但是如果我在应用程序(.NET 6.0)中包含对DLL(.NET 6.0)的引用,然后仍然动态加载它,它就可以工作。
不同之处在于.deps.json
文件 - 它列出了DLL(.NET 6.0)的依赖项。
在没有对DLL(.NET 6.0)的引用的情况下构建应用程序,然后替换deps.json
文件,使其能够工作。
我认为这可能与AssemblyLoadContext
有关,但我不知道如何使用它来加载.NET Framework的依赖项,或者不同之处是什么。
你可以在这里看到我的代码:
https://github.com/pauldeboer1987/MCCDAQ_Wrapper
我正在包装来自https://www.mccdaq.com/Software-Downloads的MCCDAQ dll。
我期望包装很多其他DLL,但这只是我的示例。我认为任何其他.NET Framework DLL都会引发这个问题,但我不能确定。
英文:
I'm loading a .NET framework DLL from an OEM's website into a .NET 6.0 wrapper DLL.
Then I load that wrapper into my .NET 6.0 Winforms app:
- App (.NET6.0) uses
- DLL (.NET6.0) uses
- DLL (.NET framework)
I get an error:
> Could not load file or assembly 'XXX, Version=1.0.9.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified
But if I include a reference to DLL (.NET6.0) in App (.NET6.0), and still load it dynamically, it works.
The difference is the .deps.json
- it lists the dependencies of the DLL (.NET6.0).
Building the app without reference to the DLL (.NET6.0) and then replacing the deps.json
file, makes it work.
I think it must be something AssemblyLoadContext
, but I don't know how to use it to load the dependencies as .NET framework. Or whatever is the difference.
You can see my code here:
https://github.com/pauldeboer1987/MCCDAQ_Wrapper
I'm wrapping the MCCDAQ dll from https://www.mccdaq.com/Software-Downloads
I expect to be wrapping many others, but this is just my example. I think any other .NET framework dll would cause this problem, but I'm not sure.
答案1
得分: 0
我添加了一个 ResolveEventHandler 到 currentDomain.AssemblyResolve,如Mattias S在 https://stackoverflow.com/questions/1373100/how-to-add-folder-to-assembly-search-path-at-runtime-in-net 中所述。这就解决了问题。请查看我的更新后的存储库。
英文:
I added a ResolveEventHandler to currentDomain.AssemblyResolve
as described by Mattias S in
https://stackoverflow.com/questions/1373100/how-to-add-folder-to-assembly-search-path-at-runtime-in-net
That does the trick. See my updated repo.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论