Using .net standard 1.5 lib in .net 4.6.2 misses System.Runtime 4.1.0.0

给你一囗甜甜゛ 提交于 2019-11-27 14:29:05

I've added a repo that shows you how to do this. From the README.md:

Requirements

Generally speaking, using libraries targeting .NET Standard in an application targeting .NET Framework requires the application project to include a NuGet reference for .NET Standard (NETStandard.Library). This ensures that the right set of assemblies are included with the application.

In Visual Studio 2015, the default way of consuming NuGet packages from .NET Framework projects is via packages.config. I don't recommend this path as this means that all assemblies are directly injected into the application project, which will significantly bloat your project file. Instead, I recommend you use project.json. To do this, perform the following steps:

  1. Uninstall all packages (if you're still using packages.config)
  2. Delete the empty packages.config
  3. Add project.json file with this content:

    json { "dependencies": { "NETStandard.Library": "1.6.0" }, "runtimes": { "win": {} }, "frameworks": { "net462": {} } }

Please note that you can generally depend on the latest version of the NETStandard.Library package, but you need to make sure to keep the framework moniker in sync with the version of .NET Framework your app is targeting, i.e. when you're targeting .NET Framework 4.6.1, you need to make sure to use net461 instead.

This feels clumsy

Yes it is. We're planning on addressing this in two ways:

  • We're replacing project.json with an MSBuild based solution in Visual Studio 2017. You'll still need to add the reference to NETStandard.Library, but you no longer have to mess with the way packages are being represented nor having to manually keep targeting information in sync.

  • We're planning to update .NET Framework so that future version of it come with built-in support for .NET Standard, in which case the reference will no longer be needed.

I found that adding the NETStandard.Library did not work for me, but ensuring that binding redirects were generated on build did the trick. For that you should ensure that you have

<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>

somewhere in your project file. This should work for console or web apps. If you're having problems running unit tests, you can use this:

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

The GenerateBindingRedirectsOutputType is necessary as the unit tests are contained in a class library which doesn't have executable output by default, so this forces any redirect configuration to be written into the build artifacts, ready to be used when the tests are executing.

You can find more details of the issues involved here: https://github.com/dotnet/announcements/issues/31

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!