Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

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

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

I could reduce the problem to this: Given:

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 };
        }
    }

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();
        }

Now I get

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

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.

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

What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...