[英]Nuget package not found after restore VS 2019 16.5.0
我整個上午都在與 nuget 斗爭,試圖獲得一個在 UI 和命令行中構建的解決方案。 這是最新的問題,我還沒有取得任何進展:
...cs(3,7,3,17): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
編譯...cs(3,7,3,17): error CS0246: The type or namespace name 'Newtonsoft' could not be found (are you missing a using directive or an assembly reference?)
我嘗試過的事情
我錯過了什么? 這不應該那么難..
更新:該解決方案包含 14 個項目:9 個 C# 類庫、2 個 c# 應用程序、1 個報告服務項目和 2 個 WiX 安裝程序項目。 所有 C# 項目都面向 Net472,而不是 Core。 解決方案結構的關鍵部分似乎是:
在構建期間,由於缺少對 Newtonsoft.Json 的引用,項目 B 無法編譯。 項目 A 和所有其他 nuget 包都作為參考提供給編譯器。 同樣,所有 nuget 包實際上都已恢復 - 項目 A 找到了 Newtonsoft.Json,而項目 B 沒有。
在詳細的 msbuild 日志輸出中,這是在項目 10(上面的項目 B)的構建中唯一提到的 Newtonsoft.Json:
10> Dependency "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed".
10> Resolved file path is "...ProjectA\bin\Release\Newtonsoft.Json.dll".
10> Reference found at search path location "...ProjectA\bin\Release".
10> For SearchPath "...ProjectA\bin\Release".
10> Considered "...ProjectA\bin\Release\Newtonsoft.Json.winmd", but it didn't exist.
10> Required by "...ProjectA\bin\Release\ProjectA.dll".
10> Required by "C:\...ProjectA2\bin\Release\ProjectA2.dll".
10> Found related file "...ProjectA\bin\Release\Newtonsoft.Json.xml".
10> The ImageRuntimeVersion for this reference is "v4.0.30319".
(文件夾和項目名稱已被隱藏)
這里發生了一些事情,終於找到了有效的解決方案。 任何人都在猜測為什么在 IDE 中內置了這個 - 它添加了一些額外的秘密武器來使事情工作(不僅僅是自動 nuget 恢復)。
要糾正這種情況:
我假設這是一種臨時情況,從長遠來看,解決方案是在任何地方使用 PackageReference。
您已經檢查了文件 app.config 和 packages.config,以及 dotnet 框架版本?
恢復 VS 2019 16.5.0 后未找到 Nuget 包
devenv /build
命令行默認沒有恢復 nuget 包的工作。 但是,VS IDE 中有這樣的選項,因此它會先恢復包,然后再構建。 但是這些在命令行中不起作用。請參閱此類似問題。
但是你仍然想使用devenv
來構建你的項目,因為你使用了一個帶有packages.config
的框架項目,我建議你可以使用nuget.exe
。 nuget.exe
這個。你可以試試這些:
1)從這個鏈接下載nuget.exe
,然后在environment variable
中將其本地地址配置為PATH,並確保可以從CMD調用nuget。
2) 打開 vs 命令提示符,cd 解決方案的路徑,然后先輸入:
nuget restore
然后你可以輸入你的 devenv 命令行,我確信這將執行而不會出現任何錯誤。
devenv xxxx.sln /rebuild
此外,您可以在解決方案的任何 xxx.csproj 文件中添加自定義目標,如下所示:
<Target Name="restoresolution" BeforeTargets="BeforeBuild">
<PropertyGroup>
<nugetpath>C:\tools</nugetpath> /////the local path of the nuget.exe
</PropertyGroup>
<ItemGroup>
<slns Include="$(MSBuildProjectDirectory)\..\**\*.sln" />
</ItemGroup>
<Exec command="$(nugetpath)\nuget restore %(slns.Identity)" />
</Target>
然后就可以直接運行devenv xxxx.sln /rebuild
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.