How can I dynamically load .NET core library with .NET framework dependencies?

Paul De Boer 0 Reputation points
2023-02-16T14:49:29.32+00:00

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)

You can see my code here:

https://github.com/pauldeboer1987/MCCDAQ_Wrapper

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.

See the yesref and noref bins in my repo

I think it must be something about how I use AssemblyLoadContext, but I don't know how to use it to load the dependencies as .NET framework. Or whatever is the difference.

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.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,927 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Paul De Boer 0 Reputation points
    2023-02-17T17:46:33.6233333+00:00

    That means it loaded the dll successfully, and then tried to open the hardware device that you don't have.

    I actually resolved my problem and pushed the fix to the repo.

    See my response in stack overflow

    https://stackoverflow.com/questions/75465012/dynamically-loaded-net-core-library-with-net-framework-dependencies-doesnt-lo

    I'm not sure why I had to implement that fix, but it works, so I'm happy.

    If you're still curious, comment out the line
    currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder);
    and it will fail.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.