Azure Function App = 在本地运行单个 Azure Function 进行调试

问题描述 投票:0回答:4

在 Visual Studio 中,我创建了一个具有多个函数的 Azure 函数应用程序。

当我从工具栏启动 Function App 调试器时,所有函数都会被触发。

有没有办法在 Visual Studio 2017 中从应用程序触发单个功能?

visual-studio azure azure-functions faas
4个回答
38
投票

实现这一目标并不容易,但这是可能的。

  1. 禁用功能:

通过修改function.json文件:

"bindings": [
...
],
"disabled": true

或使用[禁用]属性:

[Disable]
[FunctionName("Function")]
[NoAutomaticTrigger]
public static void Function(string input, TraceWriter log)
{ }
  1. 使用 Azure Core Tools 运行函数(仅限 v1.x)

使用命令运行函数:

func run <functionName>

  1. 在host.json文件中指定函数

在您的 host.json 文件中指定应运行的函数:

{ 
   "functions":[ "FunctionToRun" ]
} 

10
投票

正如@Pawel Maga 提到的,有三种方法。

我对第三个选项有更好的方法(在 host.json 文件中指定函数)。

不要搞乱 host.json (我们可能会在发布时忘记撤消。我已经这样做了很多次了:p)。

我们可以通过在 local.settings.json 中设置值来覆盖函数数组。

例如: 在 local.settings.json 中设置如下代码

{
  "Values": {
    "AzureFunctionsJobHost__functions__0":  "FunctionToRun",
    "AzureFunctionsJobHost__functions__1":  "SecondFunctionToRun",
  }
}

而不是在 host.json 中编写以下代码

{ 
   "functions":[ "FunctionToRun", "SecondFunctionToRun" ]
} 

8
投票

作为上述答案的更新:看来您可以通过这种方式禁用 localsettings.json 中的功能。

{
  "Values": {
    "AzureWebJobs.MyFirstFunction.Disabled": true,
    "AzureWebJobs.MySecondFunction.Disabled": true
  }
}

这就是我计划为我们的团队做的事情,因为它具有“安全”选项的最好语法(发布时不必撤消的方法)。

请参阅此处:https://learn.microsoft.com/en-us/azure/azure-functions/disable-function?tabs=portal#localsettingsjson


0
投票

这是目前通过终端仅运行部分功能的最简单方法:

func start --functions <space separated list of functions>

来自

func help

--functions
要加载的函数的空格分隔列表。

© www.soinside.com 2019 - 2024. All rights reserved.