Debugging .NET Assembly Load

83 views (last 30 days)
I am struggling with loading a .NET 8 library assembly into matlab. I keep getting errors like the following:
Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
After attempting the load, I check the .NET environment and I get the following:
>> dotnetenv
ans =
NETEnvironment with properties:
Runtime: framework
Status: loaded
Version: ".NET Framework 4.0.30319.42000"
RuntimeLocation: "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\"
Why is it loading the .NET Framework if the assembly is .NET 8? I created a new C#, .NET 8 library in Visual Studio. It has only one class with a couple of properties. The results above are from attempting to load this project.
Since the error occurs during loading, there's nothing for Visual Studio debugger to attach to. Is there a way to debug the assembly loading in Matlab or is this a .NET issue? The only other option I can think of is to start shotgun loading various dependencies trying to find the right sequence to load things, but that will be very painful.
EDIT
I downloaded Fusion++ and been trying to diagnose the way Matlab is attempting to load these assemblies. One thing I've discovered is a difference between a self-contained and framework-dependent build in .NET. When creating a self-contained library the error you get is:
Message: Could not load type 'System.Object' from assembly 'System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' because the parent does not exist.
Source: mscorlib
HelpLink: None
When creating a framework-dependent library it's a slightly different error.
Message: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Source: mscorlib
HelpLink: None
It seems it can find the .NET 8 runtime in the self-contained build, but fails on some subsequent dependency.

Accepted Answer

Michael VanMeter
Michael VanMeter on 10 May 2024
The solution, as it turns out, is quite simple. I needed to change the runtime from "framework" to "core" prior to calling addAssembly.
>> dotnetenv("core")
ans =
NETEnvironment with properties:
Runtime: core
Status: notloaded
Version: ""
RuntimeLocation: ""
>> NET.addAssembly(dllPath)
ans =
NET.Assembly handle
Package: NET
Properties for class NET.Assembly:
AssemblyHandle
Classes
Structures
Enums
GenericTypes
Interfaces
Delegates

More Answers (0)

Tags

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!