How to call a method in a .NET Core assembly from .NET Framework

1

I have two assemblies:

  1. The parent assembly is compiled against .NET Framework.
  2. The child assembly is compiled against .NET Core.

I want to call a method in the child assembly, and retrieve it's return object. The constructor doesn't take any parameters. The method does not take any parameters, and returns a Microsoft.OData.Edm.IEdmModel

Things I've tried so far:

  1. Creating a separate appdomain, and calling 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. Creating a class that extends MarshalByRefObject, and using that to load the assembly, call the function, and marshal the return type:
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
     }
}

In both cases, the code doesn't work because of the following exception (obtained from 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.

From what I can tell, it's trying to load System.Runtime, which fails because parent app already has a different version of System.Runtime loaded.

Is this even possible?

c#
.net
appdomain
asked on Stack Overflow Apr 29, 2019 by Nathan Merrill

2 Answers

2

.NET Core and .NET Framework are different runtimes. Try to use .NET Standard for common library and you will be able to use it from both .NET Core and .NET Framework.

answered on Stack Overflow Apr 29, 2019 by Viacheslav Rud'
2

The answer is simple: you can't call directly the .NET Core assembly from .NET Framework and vice versa. The .NET Core is not compatible with .NET Framework.

You can call the compatible assembly of .NET Core from .NET Framework assembly, as long as the assembly is targeted to use .NET Standard, preferably at least NET Standard 2.0 to provide compatibility with .NET Core 2.1 and 2.2 runtime.

See also the official doc of .NET Standard: https://docs.microsoft.com/en-us/dotnet/standard/net-standard


User contributions licensed under CC BY-SA 3.0