Search code examples
c#.netmatlabinterop

Issues loading .NET 8 Assembly into Matlab R2023a


I am struggling with loading a .NET 8, C# dynamic library assembly into Matlab.

I've tried the following:

  1. Creating a framework-dependent library. This is results in an error where Matlab cannot find System.Runtime

    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
    
  2. Creating a self-contained library. This seems to load some of the .NET 8 assemblies but eventually fails with the error:

    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
    
  3. Creating an AOT compatible, self-contained library. This throws the following error. The DLL file referenced in the warning is the DLL of my library project.

    Message: Could not load file or assembly 'file:///<DllFilePath>' or one of its dependencies. The module was expected to contain an assembly manifest.
    Source: mscorlib
    HelpLink: None
    

After attempting the load (any of the loads), I check the .NET environment in Matlab 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?

Since the error occurs during loading, there's nothing for Visual Studio debugger to attach to in order to use something like AssemblyResolve. I downloaded Fusion++ and have been trying to diagnose the way Matlab is attempting to load these assemblies.

I'm wondering if loading the System namespace by partial name is part of the problem. Is it possible that since it's not fully qualified it is finding the .NET Framework assembly in the GAC as opposed to using the .NET 8 assembly provided with the library? Is there a way to steer the Matlab loader to the correct assembly?

Here is an excerpt from the Fusion log for the System namespace.

*** Assembly Binder Log Entry  (5/9/2024 @ 12:46:00) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  <MatlabDir>\MATLAB\64_bit\R2023a\bin\win64\MATLAB.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///<MatlabDir>/MATLAB/64_bit/R2023a/bin/win64/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MATLAB.exe
Calling assembly : dotnetcli, Version=1.0.8570.32197, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: <MatlabDir>\MATLAB\64_bit\R2023a\bin\win64\MATLAB.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///<MatlabDir>/MATLAB/64_bit/R2023a/bin/win64/System.DLL.
LOG: Attempting download of new URL file:///<MatlabDir>/MATLAB/64_bit/R2023a/bin/win64/System/System.DLL.
LOG: Assembly download was successful. Attempting setup of file: <MatlabDir>\MATLAB\64_bit\R2023a\bin\win64\System\System.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
LOG: A partially-specified assembly bind succeeded from the application directory. Need to re-apply policy.
LOG: Using application configuration file: <MatlabDir>\MATLAB\64_bit\R2023a\bin\win64\MATLAB.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
LOG: Found assembly by looking in the GAC.
LOG: Binding succeeds. Returns assembly from C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll.
LOG: Assembly is loaded in default load context.

Solution

  • Call dotnetenv("core") before addassemly