dotnet build 执行implicit restore 作为命令的一部分。
这使用了dotnet restore,它不支持packages.config 中的项目引用,而这正是这个项目正在使用的。
dotnet restore 仅支持 .csproj 包引用。
这就是为什么,这个项目在 Visual Studio 中构建得非常好,但 dotnet build 会抛出错误。
您可以通过在 Visual Stduio 中右键单击文件并单击迁移来将 packages.config 迁移到包引用,但这仍然无法解决您的问题,因为 dotnet cli 可以正常与 . NET Framework 仅当项目是使用 dotnet new 命令创建的。
我假设这个项目是在 Visual Studio 中创建的,因为它有一个 Visual Studio 解决方案文件 - .sln - 因此通常具有不同的结构 .csproj 格式。
这通常会破坏一些 CLI 命令,即使在这种情况下迁移引用也是如此。
您有 2 个解决方法。
1.使用nuget restore
最简单的选择是从here 下载 NuGet CLI 可执行文件,从downloads page 获取。
如果您不在 Windows 上,请使用 this guide by Microsoft。
将其添加到您的 PATH 或将其放在项目的根文件夹中。
运行nuget restore,它与packages.config 兼容(如果您在PowerShell 中信任该命令,则运行.\nuget restore,因为默认情况下PowerShell 不会从当前位置加载命令以确保安全)。
你应该得到类似这样的输出:
PS C:\Users\StackOverflow\NotepadAndCalculatorTest> .\nuget restore
MSBuild auto-detection: using msbuild version '16.9.0.16703' from 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\bin'.
Restoring NuGet package Microsoft.WinAppDriver.Appium.WebDriver.1.0.1-Preview.
Restoring NuGet package Selenium.Support.3.8.0.
Restoring NuGet package Selenium.WebDriver.3.8.0.
Restoring NuGet package Castle.Core.4.2.1.
Restoring NuGet package MSTest.TestFramework.1.2.0.
Restoring NuGet package Newtonsoft.Json.10.0.3.
Restoring NuGet package MSTest.TestAdapter.1.2.0.
Adding package 'MSTest.TestFramework.1.2.0' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Adding package 'Selenium.Support.3.8.0' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Adding package 'Castle.Core.4.2.1' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Adding package 'Microsoft.WinAppDriver.Appium.WebDriver.1.0.1-Preview' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Adding package 'Selenium.WebDriver.3.8.0' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Adding package 'Newtonsoft.Json.10.0.3' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Adding package 'MSTest.TestAdapter.1.2.0' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Added package 'Microsoft.WinAppDriver.Appium.WebDriver.1.0.1-Preview' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Added package 'Selenium.Support.3.8.0' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Added package 'Selenium.WebDriver.3.8.0' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Added package 'Castle.Core.4.2.1' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Added package 'Newtonsoft.Json.10.0.3' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Added package 'MSTest.TestAdapter.1.2.0' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
Added package 'MSTest.TestFramework.1.2.0' to folder 'C:\Users\StackOverflow\NotepadAndCalculatorTest\packages'
NuGet Config files used:
C:\Users\StackOverflow\AppData\Roaming\NuGet\NuGet.Config
C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.FallbackLocation.config
C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config
Feeds used:
C:\Users\StackOverflow\.nuget\packages\
https://api.nuget.org/v3/index.json
C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
Installed:
7 package(s) to packages.config projects
然后运行dotnet build。
它不会尝试运行dotnet restore,因为包已经被 NuGet 恢复,所以你不会收到任何错误:
PS C:\Users\StackOverflow\NotepadAndCalculatorTest> dotnet build
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
Nothing to do. None of the projects specified contain packages to restore.
NotepadCalculatorTest -> C:\Users\StackOverflow\NotepadAndCalculatorTest\bin\Debug\NotepadCalculatorTest.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.08
2。移植项目
第二种解决方法是使用dotnet new 创建一个新项目并将代码移植到上面,以便您的.csproj 文件与dotnet restore 以及随后的dotnet build 一起使用。
除非您不想通过 NuGet 恢复,否则我会推荐选项 1。