Microsoft has introduced. NET standard in recent years to standardize and unify APIs for target platforms such as. NET Framework,.net Core,xamarin, which greatly facilitates the work of class library writers. Simply put, a class library writer can publish a library only if it is published based on. NET Standard, and the program is written to run on every target platform.
. NET standards is a standard that can run a program built on this standard API as long as the platform meets this standard.
It feels good to use, but it actually has some pits. For example, this common filenotfoundexception, when there is a situation, often appear:
The target platform for the main program is a specific platform (not. NET Standard, for example,. NET Framework 4.0), followed by an upgrade of the Framework of 4.6.1 for the introduction of new features, which also refers to a. NET Standard class library, exactly, This class library also references other package. (That is, the form of a reference a->b->c, where A is a. NET Framework program, B is a nuget package, and C is a NuGet package referenced by B.) In this case, if F5 starts the program, it will be reported FileNotFoundException.
Test conditions
Visual Studio Community
Package for test: Unifiedconfig v1.1.6
Tip no System.IO.FileSystem found. At first glance, the feeling is a simple reference error, but the Unifiedconfig package has a normal reference to this project, according to reason vs can normally help us deal with the reference problem. To the file output path, and found that the corresponding package was not copied correctly. Manually copy from the package folder to resolve the problem.
The reason is on this cross-platform. Because of the reference delivery, B cannot determine which target platform the package to a output path needs to replicate. When the program is upgraded from the old framework, the legacy project files do not work well with the. NET standard issue. But we do a manual one copy is not the way, the following gives the solution.
Solution:
- The most straightforward scenario is to modify the main project's. csproj file, which will be
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
added to the first PropertyGroup
- If not, add
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
Or
- In the tool->nuget Package Manager--Package Manager, the default package management format is changed from Packages.config to Packagereference.
Postscript
I remember that this bug existed in earlier versions of Visual Studio 2017, and after the upgrade of Visual Studio 2017 15.6.4 This version tested the new project, there is no such problem. This configuration is already added by default for. csproj files that are generated based on the 4.5.2 framework. <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
However, this problem often occurs when the old version of the project references the. NET Standard class Library, and we need to add the configuration item manually.
FileNotFoundException exploration caused by. NET Standard library References