为什么在 .NET Framework 4.5 项目上构建的 dotnet 会抛出“此项目引用了缺少的 NuGet 包”?

Sei*_*aut 1 .net c# visual-studio-code winappdriver

使用终端命令在 VS Code 中构建的示例NotepadAndCalculatorTestdotnet build项目会引发以下错误:

C:\Program Files\dotnet\sdk\5.0.401\Microsoft.Common.CurrentVersion.targets(820,5): error : The BaseOutputPath/OutputPath property is not set for project 'NotepadCalculatorTest.csproj'.  Please check to make sure that you have specified a valid combination of Configuration and Platform for this project.  Configuration='Debug'  Platform='AnyCPU'.  You may be seeing this message because you are trying to build a project without a solution file, 
and have specified a non-default Configuration or Platform that doesn't exist for this project. [C:\Users\<username>\VSCode Projects\WinAppDriverTryout\Test\Samples\C#\NotepadAndCalculatorTest\NotepadCalculatorTest.csproj]
Run Code Online (Sandbox Code Playgroud)

或者:

NotepadCalculatorTest.csproj(109,5): error : This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props.
Run Code Online (Sandbox Code Playgroud)

有人可以指出我一种可能的方法来消除错误吗?

Erm*_*ary 5

dotnet build作为命令的一部分执行隐式恢复。

这使用dotnet restore的是不支持packages.config& 中的项目引用的正是该项目正在使用的。

dotnet restore只支持.csproj包引用。

这就是为什么,这个项目在 Visual Studio 中构建得非常好,但是dotnet build会抛出错误。

可以通过右键单击 Visual Stduio 中的文件并单击“迁移”来迁移packages.config到包引用,但这仍然无法解决您的问题,因为dotnet cli仅当项目是使用dotnet new

我假设这个项目是在 Visual Studio 中创建的,因为它有一个 Visual Studio 解决方案文件 - .sln- 因此通常具有不同的结构.csproj格式。

这通常会破坏一些 CLI 命令,即使您在这种情况下迁移了引用。

您有 2 个解决方法。


1. 使用nuget restore

最简单的选择是从此处下载 NuGet CLI 可执行文件(取自下载页面)

如果您使用的不是 Windows,请使用Microsoft 的本指南

将其添加到您的 PATH 或将其放置在项目的根文件夹中。

Run nuget restore,它与packages.config(run.\nuget restore如果您在 PowerShell 中运行以信任该命令,因为出于安全考虑,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
Run Code Online (Sandbox Code Playgroud)

然后运行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
Run Code Online (Sandbox Code Playgroud)

2. 移植项目

第二个解决方法是使用并将代码移植到创建一个新项目,dotnet new以便您的.csproj文件可以与dotnet restore.dotnet build


我会推荐选项 1,除非您不想通过 NuGet 进行恢复。