将 Azure 函数项目添加到解决方案后,一些 tasks.json 文件设置已被删除

After adding an Azure Function Project to Solution some tasks.json file settings have been removed

我有一个小问题,我不确定我是否正确地将 Azure 函数项目添加到我的 workspace/solution。 我有一个解决方案,其中有 4 个项目,一个 API(DotNet Core)、客户端(Angular)、核心(class 模型和接口)、基础设施(EF、DB , 服务)。

我想向此 solution/workspace 添加一个 Azure 函数项目,我通过创建一个名为 'Azure' 的文件夹来实现,如绿色所示,然后我单击我的 Azure 扩展,然后单击 'Add New Project' 并完成了这些步骤。它创建成功,所有文件都在文件夹中。我可以 运行 函数并在其中接收消息!不错不错!

这是我的 Azure 函数扩展选项

问题 - 现在当我去调试网站应用程序 (Angular/.Net Core) 并启动 Angular 服务器然后debug/launch,我应该让编译器为我构建一个新的 API.DLL,但它没有!我看到一条错误消息弹出显示

我可以看到我的 tasks.json 文件已被修改。

这是我添加 Azure 功能之前的 tasks.json。您可以看到 API.csproj 信息

的构建标签

{
  "version": "2.0.0",
  "tasks": [{
      "label": "build",
      "command": "dotnet",
      "type": "process",
      "args": [
        "build",
        "${workspaceFolder}/API/API.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "problemMatcher": "$msCompile"
    },
    {
      "label": "publish",
      "command": "dotnet",
      "type": "process",
      "args": [
        "publish",
        "${workspaceFolder}/API/API.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "problemMatcher": "$msCompile"
    },
    {
      "label": "watch",
      "command": "dotnet",
      "type": "process",
      "args": [
        "watch",
        "run",
        "${workspaceFolder}/API/API.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "problemMatcher": "$msCompile"
    }
  ]
}

这是我添加函数项目后 tasks.json,其中 DotNet Core API 的构建标签已被删除!现在只有 Azure Functions 数据在里面。

{
  "version": "2.0.0",
  "tasks": [{
      "label": "watch",
      "command": "dotnet",
      "type": "process",
      "args": [
        "watch",
        "run",
        "${workspaceFolder}/API/API.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "problemMatcher": "$msCompile"
    },
    {
      "label": "clean",
      "command": "dotnet",
      "args": [
        "clean",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "type": "process",
      "problemMatcher": "$msCompile",
      "options": {
        "cwd": "${workspaceFolder}/Azure/Functions"
      }
    },
    {
      "label": "build",
      "command": "dotnet",
      "args": [
        "build",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "type": "process",
      "dependsOn": "clean",
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": "$msCompile",
      "options": {
        "cwd": "${workspaceFolder}/Azure/Functions"
      }
    },
    {
      "label": "clean release",
      "command": "dotnet",
      "args": [
        "clean",
        "--configuration",
        "Release",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "type": "process",
      "problemMatcher": "$msCompile",
      "options": {
        "cwd": "${workspaceFolder}/Azure/Functions"
      }
    },
    {
      "label": "publish",
      "command": "dotnet",
      "args": [
        "publish",
        "--configuration",
        "Release",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "type": "process",
      "dependsOn": "clean release",
      "problemMatcher": "$msCompile",
      "options": {
        "cwd": "${workspaceFolder}/Azure/Functions"
      }
    },
    {
      "type": "func",
      "dependsOn": "build",
      "options": {
        "cwd": "${workspaceFolder}/Azure/Functions/bin/Debug/net5.0"
      },
      "command": "host start",
      "isBackground": true,
      "problemMatcher": "$func-dotnet-watch"
    }
  ]
}

仅供参考 看起来我的 tasks.json 文件都经过调整和修改! 看起来 dotnet 构建属性已被 Azure 函数属性覆盖。如果我复制并粘贴回旧的 tasks.json 文件,它可以正常工作。添加这个新的 Azure 函数项目是否应该删除了应用程序其他部分的所有属性?

问题 - 这似乎不是将 Azure 函数项目添加到现有 workspace/solution 的最佳方式。那么规定的方法是什么?是否有教程或 link 我可以遵循的地方,因为 Microsoft 站点上的所有内容都单独显示新的独立函数项目。

一些快速挖掘,我在 vscode-azurefunctions 存储库中找到了这个

private insertNewTasks(existingTasks: ITask[] | undefined, newTasks: ITask[]): ITask[] {
    existingTasks = existingTasks || [];
    // Remove tasks that match the ones we're about to add
    existingTasks = existingTasks.filter(t1 => !newTasks.find(t2 => {
        if (t1.type === t2.type) {
            switch (t1.type) {
                case func:
                    return t1.command === t2.command;
                case 'shell':
                case 'process':
                    return t1.label === t2.label && t1.identifier === t2.identifier;
                default:
                    // Not worth throwing an error for unrecognized task type
                    // Worst case the user has an extra task in their tasks.json
                    return false;
            }
        } else {
            return false;
        }
    }));
    existingTasks.push(...newTasks);
    return existingTasks;
}

从外观上看,扩展将尝试替换 typelabelidentifier 中匹配的现有任务,而不是尝试扩展它们;在我看来,对于像 processbuild.

这样常见的事情,这样做是明智的

所以你是对的,你的 tasks.json 正在使用扩展提供的向导在创建新的 func 项目时由扩展更新。老实说,我认为这是一个错误,您可能想直接向他们报告 at the vscode-azurefunctions repo. Also, the code in question can be found here

将问题报告为错误并等待解决的替代方法:

通过 Func Core Tools,您可以使用 func new 从您想要在其中放置函数的文件夹(使用您喜欢的终端)。它会让您选择工作运行时(dotnet、js、python 等)和项目名称。它不会干扰您现有的 tasks.json.