Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a .Net Framework 4 (or Mono) assembly from a .Net Core application

I have a simple .Net Core Console application referencing a .Net Framework 4.6.2 project in the same solution. When I build, I get the following error:

error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

Am I trying to do something that is not allowed?

Is there some way to reference a .Net Framework 4.6 (or Mono) assembly (or P/Invoke it) from a .Net Core application?

Using Visual Studio 2017, .Net Core 1.1

like image 236
Verax Avatar asked Mar 10 '17 12:03

Verax


People also ask

Can you call .NET framework from .NET Core?

No, there is no way to load/run that . NET Framework assembly on . NET Core 1.1, as there is no "compatibility shim" in . NET Core 1.1.

Does .NET Core support remoting?

NET Remoting isn't supported on . NET 5+ (and . NET Core). . NET remoting was identified as a problematic architecture.

Is .NET Core different from .NET framework?

NET Core and . NET Framework is a platform for . NET applications on Windows whereas, NET Core is the latest version of the . NET Framework which is a cross-platform and open-source framework optimized for modern app needs and developer workflows.

How is .NET Core cross-platform?

NET Core is cross-platform. It runs on Windows, OS X and multiple distributions of Linux. It also supports different CPU architectures. We're adding more Linux distribution and CPU architecture support with the eventual goal of .


2 Answers

In .NET Core 1.x, you are not allowed to reference a .NET Framework 4.6 (or Mono) assembly. .NET Core 1.x and .NET Framework are 2 different runtimes, and their types don't mix - like the build error message says.

Instead, what you can do is to recompile that .NET Framework 4.6 assembly targeting ".NET Standard". Read more about .NET Standard. Compiling a library for .NET Standard allows that library to run on .NET Framework, Mono, or .NET Core. Another option is to multi-target that .NET Framework 4.6 assembly, so it builds both for .NET Framework 4.6 and for .NET Core 1.1. You should only do this if the APIs your library requires are not supported by .NET Standard. This is roughly the same as @VMAtm's answer, but you can do it with a single .csproj in VS 2017.

The good news is, what you are trying to do will be supported on .NET Core 2.0, which is slated for release later this year.

like image 176
Eric Erhardt Avatar answered Oct 19 '22 14:10

Eric Erhardt


.Net Core and .Net 4.0 has different core libraries. The type System.Object from old .Net libraries is defined in mscorlib, which clearly hasn't been referenced.

If you do reference that library, you probably will get the name resolving error, because the same types will be defined in the same namespaces and with same type names in different libraries. So yes, this is not allowed.

Edit: However, there is a common approach known as Shared project, you can read about it here. Also this quaestion may be useful for you:

.net core classlibrary calling .net framework class library

Also you may try to create a portable class library, which can be referenced either from .Net Core and .Net 4.6 library.

Portable class libraries are great when your common code is platform-independent as well as for reusable libraries where the platform-specific code can be factored out.

Edit #2: As @EricErhardt mentioned, you can recompile your .Net 4.6 library with different targeting, which may be not an option if you don't have a source code.

like image 24
VMAtm Avatar answered Oct 19 '22 15:10

VMAtm