在 .net 4.6.2 中使用 .net standard 1.5 lib 未命中 System.Runtime 4.1.0.0

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

我在 .net Framework 4.6.2 consoleapps 中使用 .net standard 时遇到一些问题。

我可以将问题简化为: 给定:

我用这个 class

创建了一个 .net 标准 1.5 客户端库 vs 2017
public class Class1
    {
        public List<int> Get()
        {
            return new List<int>() { 1, 2, 3, 4, 5, 65, 6 };
        }
    }

现在我创建了一个新的 .net 4.6.2 控制台应用程序,它只是调用这个 class:

的方法
       static void Main(string[] args)
        {
            var foo = new Class1();

            Console.WriteLine("Done!");
            Console.ReadLine();
        }

现在我得到

System.IO.FileNotFoundException: 'The File or Assembly "System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a was not found

当我将 .net standardlib nuget 包添加到 .net fx 控制台时,它起作用了。但是 system.runtime 将可以通过 GAC 和 nuget 参考访问,这对我来说似乎很丑陋。

我在这里推送了这个简短的测试解决方案:https://github.com/Gentlehag/NetStandardSample

我错过了什么?

我添加了一个 repo 来向您展示如何执行此操作。来自 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.

我发现添加 NETStandard.Library 对我不起作用,但确保在构建时生成绑定重定向可以解决问题。为此你应该确保你有

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

项目文件中的某处。这应该适用于控制台或网络应用程序。如果你有问题 运行 单元测试,你可以使用这个:

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

GenerateBindingRedirectsOutputType 是必需的,因为单元测试包含在默认情况下没有可执行输出的 class 库中,因此这会强制将任何重定向配置写入构建工件, 准备在测试执行时使用。

您可以在此处找到所涉及问题的更多详细信息:https://github.com/dotnet/announcements/issues/31