将.NET 8程序集加载到Matlab R2023a时遇到的问题
在MATLAB中加载.NET 8 C#动态库的问题及排查
我尝试将.NET 8的C#动态库加载到MATLAB时遇到了问题,以下三种方案均失败:
- 依赖框架的库:MATLAB找不到
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
- 自包含库:加载部分.NET 8程序集后仍失败,错误信息:
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
- 兼容AOT的自包含库:报错如下(警告中引用的DLL为我的库项目文件):
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
加载失败后,我检查了MATLAB的.NET环境,结果如下:
>> 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\"
核心疑问
为什么我的程序集是.NET 8,但MATLAB却加载.NET Framework?
由于错误发生在加载阶段,无法通过Visual Studio调试器附加使用AssemblyResolve等方法。我已下载Fusion++来诊断MATLAB加载程序集的方式。
我怀疑问题出在通过部分名称加载System命名空间:是否因为未使用完全限定名称,导致MATLAB在GAC中找到.NET Framework的程序集,而非我的库附带的.NET 8程序集?有没有方法引导MATLAB加载正确的程序集?
以下是System命名空间的Fusion日志片段:
*** 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.
内容的提问来源于stack exchange,提问作者BoilermakerRV
相关产品推荐
相关产品推荐

