首页 > 解决方案 > 为什么 dotnet 在 .NET Framework 4.5 项目上构建会抛出“此项目引用了缺失的 NuGet 包”?

问题描述

使用终端命令在 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]

或者:

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.

有人可以指出我摆脱错误的可能方法吗?

标签: c#.netvisual-studio-codewinappdriver

解决方案


dotnet build作为命令的一部分执行隐式还原。

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

dotnet restore仅支持.csproj包引用。

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

可以通过右键单击 Visual Stduio 中的文件并单击迁移来迁移packages.config到包引用,但这仍然无法解决您的问题,因为dotnet cli只有在使用dotnet new命令创建项目时才能与 .NET Framework 一起正常工作。

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

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

您有 2 个解决方法。


1.使用nuget restore

最简单的选择是从下载页面下载 NuGet CLI 可执行文件。

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

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

Run nuget restore,它与兼容packages.config.\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

然后运行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。


推荐阅读