.NET 8类库导出函数在.NET Framework等环境调用失败求助
我尝试从.NET 8.0类库中导出函数,使用3F DLL Export项目实现,类库代码如下:
public class Class1 { [DllExport] public static int AddOne(int x) { Console.WriteLine("Inside AddOne"); return x + 1; } }
.NET Core和.NET Framework测试程序代码如下:
static void Main(string[] args) { Console.WriteLine("Hello, DotNetCore"); try { Console.WriteLine("1+1=" + AddOne(1)); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.ReadKey(); } [DllImport("ClassLibrary.dll")] private static extern int AddOne(int x);
调用时出现以下错误:
.NET Core环境错误
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at ConsoleCore.Program.AddOne(Int32 x)
at ConsoleCore.Program.Main(String[] args) in C:\work\stackoverflow\core and framework\Console\ConsoleCore\Program.cs:line 12
.NET Framework环境错误
System.IO.FileNotFoundException: 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.
File name: 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at ConsoleApp.Program.AddOne(Int32 x)
at ConsoleApp.Program.Main(String[] args) in C:\work\stackoverflow\core and framework\Console\Console\Program.cs:line 17=== Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
(Fully-specified)
LOG: Appbase = file:///C:/work/stackoverflow/core and framework/Console/Console/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\work\stackoverflow\core and framework\Console\Console\bin\Debug\Console.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.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: Attempting download of new URL file:///C:/work/stackoverflow/core and framework/Console/Console/bin/Debug/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/work/stackoverflow/core and framework/Console/Console/bin/Debug/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/work/stackoverflow/core and framework/Console/Console/bin/Debug/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/work/stackoverflow/core and framework/Console/Console/bin/Debug/System.Runtime/System.Runtime.EXE.
最终需求
让.NET 8.0类库通过P/Invoke被.NET Standard 2.0库调用(因依赖问题无法升级该库),再由该.NET Standard 2.0库为.NET Core和.NET Framework应用提供服务。
内容的提问来源于stack exchange,提问作者White

