Dynamically loaded .NET core library with .NET framework dependencies doesn't load the successfully, but in project reference does

huangapple go评论50阅读模式
英文:

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应用程序中:

  1. 应用程序(.NET 6.0)使用
  2. DLL(.NET 6.0)使用
  3. 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:

  1. App (.NET6.0) uses
  2. DLL (.NET6.0) uses
  3. 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.

huangapple
  • 本文由 发表于 2023年2月16日 04:12:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定