0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Why do you expect .NET Framework DLL to be loadable into .NET 6 app? .NET Framework and .NET 6 are different incompatible frameworks. – Vlad DX Feb 15 '23 at 20:21
  • I don't expect it to work, I need it to work. I have a Core application that must be able to use different hardware. Very many of these hardware manufacturers haven't bothered to update to .Net Core. As far as them being incompatible, the .NET6.0 app works fine when I reference the .NET framework DLL directly. Its only the wrapper that has a problem. But it needs to be wrapped to fit into my architecture. – Paul de Boer Feb 15 '23 at 20:34
  • What is a "wrapper DLL"? Why do you call it "wrapper"? How do you wrap the .NET Framework DLL into it? – Vlad DX Feb 15 '23 at 20:37
  • wrap, like a present. See my code on github. Its simply a DLL that contains the other DLL. Maybe its not the right term, since its usually used in the context of wrapping the methods and classes. Like wrapping a CDLL into a Python module. – Paul de Boer Feb 15 '23 at 20:42
  • @VladDX That _usually_ works. You can load most .NET Framework libraries into the .NET 6.0 runtime. – PMF Feb 15 '23 at 20:42
  • I don't see any DLLs there on GitHub. – Vlad DX Feb 15 '23 at 20:43
  • This doesn't "wrap" a DLL just references it: `C:\Program Files (x86)\Measurement Computing\DAQ\MccDaq.dll `. But if there is no DLL by this path, it won't work. – Vlad DX Feb 15 '23 at 20:45
  • You can include the `MccDaq.dll` as a file into your .NET library. Then you should be able to load it. – Vlad DX Feb 15 '23 at 20:46
  • It'll be a wrapper when I can get this working. i've pared this down to the most basic example. The DLL can be downloaded here: https://www.mccdaq.com/Software-Downloads I have tried putting the dll directly in with the referencing DLL and app, but that doesn't work. If I replace the deps.json, that seems to be the difference. I've updated the repo so that the bins are included. see the yesref and noref folders. The difference is the deps.json s – Paul de Boer Feb 15 '23 at 21:02
  • 1
    It is just a file-not-found error. If the assembly reference is not explicitly named in the .net6 project then msbuild does not know that the assembly needs to be copied to the final .exe project's build directory. You then have to help and copy the file yourself. Automate that with xcopy in the .exe project's post-build event. – Hans Passant Feb 15 '23 at 22:42
  • Nope, see my GITHUB example. The dependency DLLs are in the folder. – Paul de Boer Feb 16 '23 at 14:33

1 Answers1

0

I added a ResolveEventHandler to currentDomain.AssemblyResolve

as described by Mattias S in How to add folder to assembly search path at runtime in .NET?

That does the trick. See my updated repo.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 21 '23 at 02:01