簡體   English   中英

當我嘗試啟動 C# 代碼時收到消息“找不到任務‘構建’”

[英]I get the message ''could not find the task 'build'" when I try to start my C# code

我是 C# 新手,正在學習使用復數

我完全遵循了教程正在做的事情,但遇到了教程沒有的問題。

當您通過 .Net 編寫 C# 代碼時,我構建了簡單的“Hello World”程序

在Visual Studio Code中,如果選擇Start Debugging或Start without Debugging,會彈出如下錯誤

''找不到任務 'build'"

然后我還是點擊調試然后出現以下錯誤

launch:program dir \\ < insert-project-name-here >.dll不存在有打開launch.json的選項

令人困惑的是,.dll 文件確實存在......

此外,檢查launch.json,我有以下內容:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Core Launch (console)",
      "type": "coreclr",
      "request": "launch",
      "WARNING01": "*********************************************************************************",
      "WARNING02": "The C# extension was unable to automatically decode projects in the current",
      "WARNING03": "workspace to create a runnable launch.json file. A template launch.json file has",
      "WARNING04": "been created as a placeholder.",
      "WARNING05": "",
      "WARNING06": "If OmniSharp is currently unable to load your project, you can attempt to resolve",
      "WARNING07": "this by restoring any missing project dependencies (example: run 'dotnet restore')",
      "WARNING08": "and by fixing any reported errors from building the projects in your workspace.",
      "WARNING09": "If this allows OmniSharp to now load your project then --",
      "WARNING10": "  * Delete this file",
      "WARNING11": "  * Open the Visual Studio Code command palette (View->Command Palette)",
      "WARNING12": "  * run the command: '.NET: Generate Assets for Build and Debug'.",
      "WARNING13": "",
      "WARNING14": "If your project requires a more complex launch configuration, you may wish to delete",
      "WARNING15": "this configuration and pick a different template using the 'Add Configuration...'",
      "WARNING16": "button at the bottom of this file.",
      "WARNING17": "*********************************************************************************",
      "preLaunchTask": "build",
      "program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",
      "args": ["link"],
      "cwd": "${workspaceFolder}",
      "console": "internalConsole",
      "stopAtEntry": false
    },
    {
      "name": ".NET Core Attach",
      "type": "coreclr",
      "request": "attach",
      "processId": "${command:pickProcess}"
    }
  ]
}

簡單的方法

在 VS Code 中,加載代碼庫后。 調出命令托盤並輸入> .NET Generate Assets for Build and Debug然后點擊回車/回車。

這將強制 VS Code 重新創建launch.jsontask.json文件。

留意 VS Code 窗口的左下角是否有一條吐司消息,如下所示:

VS Code 的屏幕截圖,顯示了在重新創建構建和調試資產時要求確認的 Toast 消息

並確保您單擊Yes

復雜的方式

您需要更改task.json文件中當前讀取的行:

"program": "${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",

<insert-target-framework-here>替換為 csproj 中的目標框架名稱,它可能類似於以下內容:

<TargetFramework>netcoreapp2.1</TargetFramework>

您還需要將<insert-project-name-here>.dll字符串替換為您生成的 DLL 的名稱。

我有一個類似的問題,傑米泰勒的回答從一開始就沒有為我做,它一直說“找不到任務‘構建’”,我無法評論他的回答,所以這對我有用。

簡答
在調用> .NET Generate Assets for Build and Debug之前,遞歸地刪除解決方案根目錄中的任何 .vscode 文件夾(如果有)。

長答案
我每次都在 OmniSharp 日志中查找第一個錯誤(在輸出窗格中,OmniSharpLog 下拉列表中):

  • 我的第一個錯誤是 [失敗]:無法找到后備包文件夾“C:\\Microsoft\\Xamarin\\NuGet\\”
    • 解決方案 => mkdir C:\\Microsoft\\Xamarin\\NuGet\\
  • 然后下一個失敗是關於 Omnisharp 無法恢復我的 nuget 包
    • 解決方案 => cd 到我的 .sln 所在的文件夾並稱為dotnet restore

OmniSharp 日志中不再出現故障,但它仍然抱怨構建任務。 就我而言,我注意到我有兩個 .vscode 文件夾,一個在根目錄,一個在我的解決方案中的另一個文件夾中:
- 刪除了所有這些
- 稱為> .NET Generate Assets for Build and Debug
它成功了,並看到它正確創建了launch.jsontasks.json

現在我可以正確調試了。

這聽起來可能很奇怪,但在我的情況下,當我按 Ctrl + Shift + B更新我的 Visual Studio Code 后,構建操作成功進行。

在 Visual Studio 上,您可以使用 ac# 擴展的生成器來構建 launch.json 和 task.json。但是,您還應該使用 C# 擴展的生成器來生成 launch.json 和 tasks.json。 讀這個: 在這里做這個。 但是,如果您是 c# 的初學者,最好使用 Visual Studio。 所有工具都集成在一起。 它不是編輯文本,而是 IDE。 但是,如果您更喜歡 Visual Studio 代碼,請閱讀我發送的文檔:)。 視覺工作室ide

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM