reference .Net framework 4.5.2 from .Net Core 2.0 project

4

I have spent a few hours trying to figure out why .Net Core 2.0 wouldn't load .Net framework 4.5.2 nuget packages.

Now I think it's time to ask...

What happens is I have a .Net Core 2.0 WebApi app (A) and I want to reuse my .Net framework 4.5.2 library(B).

I created nuget packages for B and referenced the nuget package in the A.

The libraries are downloaded in the C:\Users\username.nuget\packages folder But when I consume B in the code, it errors me out saying

System.IO.FileLoadException: 'Could not load file or assembly 'xxx.TradeServices.Common, Version=2.1.289.1, Culture=neutral, PublicKeyToken=null'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'

I then added some dummy code

var client = new TradeServicesClient(EnvironmentType.Production);

xxx.TradeServices.Common is loaded.

But another exception appear saying cannot load dependency dll.

FileNotFoundException: Could not load file or assembly 'Ice, Version=3.7.0.0, Culture=neutral, PublicKeyToken=0c5ebb72d74932c6'. The system cannot find the file specified.

I have checked all the dependencies, they are downloaded correctly in the

C:\Users\username.nuget\packages

folder

I am not sure about why .Net Core is behaving this way. It seems the dependency chain is not loaded. Could you please help me to figure out what's going on?

.net
asp.net-core-2.0
.net-framework-version
asked on Stack Overflow Mar 28, 2018 by Larry • edited Mar 28, 2018 by dtlvd

2 Answers

4

asp.net core can target multiple frameworks; a 2.0 web app will typically run on either "netcoreapp2.0" (a .NET Core application) or "net461" (a .NET application), for example - as specified by the <TargetFramework> in the csproj. It is this <TargetFramework> that determines how all the downstream package resolution will work. If it is "net461", it may be happy to take a "net452" library. However, "netcoreapp2.0" will not want "net452" - instead preferring "netstandard2.0" or "netstandard1.6", etc. Targeting .NET Standard will mean that all downstream packages also need to target .NET Standard, which is not always possible.

So:

  • if possible, make your dependencies target some version of .NET Standard
  • if that isn't possible, ensure that the <TargetFramework> is "net461" or similar

Edit: it looks like the default projects also change between Microsoft.AspNetCore (when targeting .NET) and Microsoft.AspNetCore.All (when targeting .NET Core) - so you may also need to change that <PackageReference ... /> entry in the csproj. If you are using any of the extra packages in Microsoft.AspNetCore.All that aren't in Microsoft.AspNetCore - you may need to add the ones that you need manually.

answered on Stack Overflow Mar 28, 2018 by Marc Gravell • edited Mar 28, 2018 by Marc Gravell
0

Related answer: https://stackoverflow.com/a/54116368/10891544

To target multiple frameworks from a .net core project, open up your .csproj file and replace the TargetFramework tag with a new TargetFrameworks tag:

<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>

This should allow you to use .NET Framework packages from a .NET Core project. However, you will lose the ability to run your code cross-platform.

answered on Stack Overflow Jan 9, 2019 by Matt H

User contributions licensed under CC BY-SA 3.0