reference .Net framework 4.5.2 from .Net Core 2.0 project

后端 未结 2 1109
栀梦
栀梦 2021-01-19 23:27

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 hap

相关标签:
2条回答
  • 2021-01-20 00:01

    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.

    0 讨论(0)
  • 2021-01-20 00:16

    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.

    0 讨论(0)
提交回复
热议问题