在 .net 4.6.2 中使用 .net 标准 1.5 库会错过 System.Runtime 4.1.0.0 [英] Using .net standard 1.5 lib in .net 4.6.2 misses System.Runtime 4.1.0.0

查看:19
本文介绍了在 .net 4.6.2 中使用 .net 标准 1.5 库会错过 System.Runtime 4.1.0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .net framework 4.6.2 控制台应用程序中使用 .net 标准时遇到一些问题.

I've some problem when using .net standard in .net framework 4.6.2 consoleapps.

我可以将问题简化为:鉴于:

I could reduce the problem to this: Given:

我使用这个单一类创建了一个 .net 标准 1.5 客户端库 vis vs 2017

I create a .net standard 1.5 client library vis vs 2017 with this single class

public class Class1
    {
        public List<int> Get()
        {
            return new List<int>() { 1, 2, 3, 4, 5, 65, 6 };
        }
    }

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

Now I create a new .net 4.6.2 console application which is just calling the method of this class:

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

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

现在我明白了

System.IO.FileNotFoundException: '文件或程序集System.Runtime,版本=4.1.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a 未找到

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 参考获得,这对我来说似乎很丑陋.

When I add the .net standardlib nuget package to the .net fx console it works. but then the system.runtime would be aviable via GAC and via nuget reference which seems to be quite ugly for me.

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

I pushed this short test solution here: https://github.com/Gentlehag/NetStandardSample

我错过了什么?

推荐答案

我添加了一个 repo这向您展示了如何做到这一点.来自 README.md:

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

一般来说,在应用程序中使用面向 .NET Standard 的库面向 .NET Framework 要求应用程序项目包含 NuGet.NET Standard (NETStandard.Library) 的参考.这确保了正确的应用程序中包含一组程序集.

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.

在 Visual Studio 2015 中,从 .NET 使用 NuGet 包的默认方式框架项目是通过 packages.config 实现的.我不推荐这条路这意味着所有程序集都直接注入到应用程序中项目,这将显着膨胀您的项目文件.相反,我建议你使用 project.json.为此,请执行以下步骤:

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. 卸载所有软件包(如果您仍在使用 packages.config)
  2. 删除空的packages.config
  3. 添加具有以下内容的 project.json 文件:

json{依赖关系":{"NETStandard.Library": "1.6.0"},运行时":{赢": {}},构架": {net462":{}}}

请注意,您通常可以依赖最新版本的NETStandard.Library 包,但你需要确保保留框架名称与您的应用面向的 .NET Framework 版本同步,即当您面向 .NET Framework 4.6.1 时,您需要确保使用net461 代替.

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.

是的.我们计划通过两种方式解决这个问题:

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

  • 我们正在用 Visual 中基于 MSBuild 的解决方案替换 project.jsonStudio 2017.您仍然需要添加对 NETStandard.Library 的引用,但是您不再需要弄乱包裹的表示方式,也不必必须手动保持定位信息同步.

  • 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.

我们计划更新 .NET Framework,以便它的未来版本随附对 .NET Standard 的内置支持,在这种情况下,参考将不再需要.

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.

这篇关于在 .net 4.6.2 中使用 .net 标准 1.5 库会错过 System.Runtime 4.1.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆