【问题标题】:How to call a method in a .NET Core assembly from .NET Framework如何从 .NET Framework 调用 .NET Core 程序集中的方法
【发布时间】:2023-03-19 16:58:02
【问题描述】:

我有两个程序集:

  1. 父程序集是针对 .NET Framework 编译的。
  2. 子程序集是针对 .NET Core 编译的。

我想调用子程序集中的一个方法,并检索它的返回对象。构造函数不带任何参数。该方法不带任何参数,返回一个Microsoft.OData.Edm.IEdmModel

到目前为止我尝试过的事情:

  1. 创建一个单独的appdomain,并调用domain.CreateInstanceAndUnwrap
AppDomainSetup setup = new AppDomainSetup()
{
   ApplicationBase = path
   PrivateBinPath = path
};
AppDomain domain = AppDomain.CreateDomain("Child", null, setup);
AssemblyName assemblyName = AssemblyName.GetAssemblyName(assemblyPath);
object instance = domain.CreateInstanceAndUnwrap(assemblyName.FullName, className);
  1. 创建一个扩展 MarshalByRefObject 的类,并使用它来加载程序集、调用函数并封送返回类型:
AppDomain domain = AppDomain.CreateDomain("Child");
BinaryMarshal marshal = (BinaryMarshal) domain.CreateInstanceAndUnwrap(typeof(BinaryMarshal).Assembly.FullName, typeof(BinaryMarshal).FullName);
IEdmModel model = marshal.LoadEdmModel(path, className, functionName);

//BinaryMarshal.cs:
internal class BinaryMarshal : MarshalByRefObject
{
    public IEdmModel LoadEdmModel(string binary, string className, string functionName)
    {
        Assembly assembly = Assembly.LoadFrom(binary);
        Type type = assembly.GetType(className);  //returns null because of exception listed below
     }
}

在这两种情况下,由于以下异常(从Fuslogvw.exe 获得),代码都不起作用:

*** Assembly Binder Log Entry  (4/29/2019 @ 10:34:48 AM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\develop\parent\out\debug-amd64\Parent.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/develop/parent/out/debug-amd64/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = Parent.exe
Calling assembly : Child, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\develop\parent\out\debug-amd64\Parent.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=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/develop/parent/out/debug-amd64//System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/develop/parent/out/debug-amd64//System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/develop/parent/out/debug-amd64//System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/develop/parent/out/debug-amd64//System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/develop/child/out/debug-amd64/netcoreapp2.0/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/develop/child/out/debug-amd64/netcoreapp2.0/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/develop/child/out/debug-amd64/netcoreapp2.0/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/develop/child/out/debug-amd64/netcoreapp2.0/System.Runtime/System.Runtime.EXE.
LOG: All probing URLs attempted and failed.

据我所知,它正在尝试加载 System.Runtime,但由于父应用已经加载了不同版本的 System.Runtime,因此失败。

这可能吗?

【问题讨论】:

    标签: c# .net appdomain


    【解决方案1】:

    .NET Core 和 .NET Framework 是不同的运行时。尝试使用 .NET Standard 作为通用库,您将能够在 .NET Core 和 .NET Framework 中使用它。

    【讨论】:

    【解决方案2】:

    答案很简单:您不能直接从 .NET Framework 调用 .NET Core 程序集,反之亦然。 .NET Core 与 .NET Framework 不兼容。

    您可以从 .NET Framework 程序集调用 .NET Core 的兼容程序集,只要该程序集的目标是使用 .NET Standard,最好至少 NET Standard 2.0 以提供与 .NET Core 2.1 和 2.2 运行时的兼容性。

    另见 .NET Standard 官方文档:https://docs.microsoft.com/en-us/dotnet/standard/net-standard

    【讨论】:

    • 当您说“只要程序集以使用 .NET Standard 为目标”时,您指的是子程序集还是父程序集?
    • @NathanMerrill 是的。
    • “是”是什么意思?两个程序集都必须针对 .NET Standard?
    • 如果您希望您的程序集同时兼容 .NET Core 和 .NET Framework,则应使用 .NET Standard 作为程序集的主要目标。
    猜你喜欢
    • 2017-07-31
    • 1970-01-01
    • 2019-01-14
    • 2020-10-27
    • 2021-09-24
    • 1970-01-01
    • 2021-02-24
    • 2020-01-10
    • 1970-01-01
    相关资源
    最近更新 更多