Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 4.7.1 project can't use .NET Core 2.0 library

I've searched a lot and can't find a solution to my problem (I've seen similar runtime issues but not build).

I have a .NET 4.7.1 project (class lib) that references a .NET Core project/library. When I try to build I get the following build error:

'System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Runtime' with identity 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

The code is simple and is just calling an async method in the .NET Core lib. A bit like:

return _dotNetCoreClass.MethodAsync();

I've tried upgrading the project so it uses ProjectReferences and not packages.config. I've installed the System.Runtime package (version 4.3.0). I've updated my project file to include the below:

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

I've also installed the latest NETStandard.Librabry package but I can't get rid of the build error.

like image 743
Ben Thomson Avatar asked Mar 19 '18 11:03

Ben Thomson


People also ask

Can a .NET Core library be used by .NET Framework?

The answer is no, we cannot use . NET Framework Base Class Library in . NET Core because of compatibility issues.

Can you mix .NET Framework and .NET Core?

You cannot directly reference a . NET Framework assembly from . NET Core or the other way round a . NET Core assembly from .

Can I use a .NET Framework library with .NET 6?

Yes, you can load . NET Framework assemblies into . NET Core 5 and 6.


2 Answers

I thought it was important to post the solution I found and to highlight where my understanding went wrong (thanks to @jeroen-mostert and @hans-passant for steering me in the right direction).

Not all of the .NET Core 2.0 framework is supported by .NET 4.7.1 and so some of the code we were using was not supported and hence raised a build error in application referencing the .NET Core library.

Changing the .NET Core library to use .NET Standard 2.0 (which is fine because our other .NET Core applications that will also use the library are supported by .NET Standard) and then removing and re-adding the references in the application using it solved our problems and the application now builds.

like image 79
Ben Thomson Avatar answered Oct 05 '22 23:10

Ben Thomson


There is an issue tracked on git hub regarding the .Net Core SDK at the moment, as stated at the official .Net framework 4.7.1 disucssion:

It turns out there is an issue with .NET Core SDK, the issue is being tracked on GitHub, https://github.com/dotnet/sdk/issues/1738. By accident this issue does not repro with Visual Studio 15.5 release. If anybody would like to see if there is any workaround before the SDK issue is fixed, feel free to file an issue on https://github.com/aspnet/Mvc/issues.

For the moment, there is no way to fix your solution as 4.7.1 cannot support .Net Core.

like image 40
Barr J Avatar answered Oct 06 '22 01:10

Barr J