如何在 Azure Functions 中禁用函数How to disable functions in Azure Functions

本文介绍如何在 Azure Functions 中禁用函数。This article explains how to disable a function in Azure Functions. 禁用某个函数意味着运行时将忽略针对该函数定义的自动触发器。To disable a function means to make the runtime ignore the automatic trigger that's defined for the function. 这使你可以在不停止整个函数应用的情况下阻止特定函数运行。This lets you prevent a specific function from running without stopping the entire function app.

若要禁用函数,建议使用 AzureWebJobs.<FUNCTION_NAME>.Disabled 格式的应用设置。The recommended way to disable a function is by using an app setting in the format AzureWebJobs.<FUNCTION_NAME>.Disabled. 可以通过多种方式创建和修改此应用程序设置,包括使用 Azure CLI,以及使用 Azure 门户中函数的“管理”选项卡。You can create and modify this application setting in a number of ways, including by using the Azure CLI and from your function's Manage tab in the Azure portal.

备注

如果使用本文所述的方法禁用 HTTP 触发的函数,则在本地计算机上运行时,仍然可以访问该终结点。When you disable an HTTP triggered function by using the methods described in this article, the endpoint may still by accessible when running on your local computer.

使用 Azure CLIUse the Azure CLI

在 Azure CLI 中,可以使用 az functionapp config appsettings set 命令来创建和修改应用设置。In the Azure CLI, you use the az functionapp config appsettings set command to create and modify the app setting. 以下命令通过创建名为“AzureWebJobs.QueueTrigger.Disabled”的应用设置并将其设置为 true 来禁用名为“QueueTrigger”的函数。The following command disables a function named QueueTrigger by creating an app setting named AzureWebJobs.QueueTrigger.Disabled set it to true.

az functionapp config appsettings set --name <myFunctionApp> \
--resource-group <myResourceGroup> \
--settings AzureWebJobs.QueueTrigger.Disabled=true

若要重新启用此函数,请使用值 false 重新运行同一命令。To re-enable the function, rerun the same command with a value of false.

az functionapp config appsettings set --name <myFunctionApp> \
--resource-group <myResourceGroup> \
--settings AzureWebJobs.QueueTrigger.Disabled=false

使用门户Use the Portal

还可以使用函数的“概览”页上的“启用”和“禁用”按钮。You can also use the Enable and Disable buttons on the function's Overview page. 这两个按钮通过创建和删除 AzureWebJobs.<FUNCTION_NAME>.Disabled 应用设置来工作。These buttons work by creating and deleting the AzureWebJobs.<FUNCTION_NAME>.Disabled app setting.

函数状态开关

备注

门户集成的测试功能会忽略 Disabled 设置。The portal-integrated testing functionality ignores the Disabled setting. 这意味着,从门户的“测试”窗口启动时,禁用的函数仍会运行。This means that a disabled function still runs when started from the Test window in the portal.

local.settings.jsonlocal.settings.json

在本地运行时,可以用相同的方式禁用函数。Functions can be disabled in the same way when running locally. 若要禁用名为 HttpExample 的函数,请在 local.settings.json 文件的“值”集合中添加一个条目,如下所示:To disable a function named HttpExample, add an entry to the Values collection in the local.settings.json file, as follows:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "java",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true", 
    "AzureWebJobs.HttpExample.Disabled": "true"
  }
}

其他方法Other methods

虽然我们建议你将应用程序设置方法用于所有语言和所有运行时版本,但是你也可以使用其他几种方法来禁用函数。While the application setting method is recommended for all languages and all runtime versions, there are several other ways to disable functions. 这些方法因语言和运行时版本而异,为了后向兼容而保留。These methods, which vary by language and runtime version, are maintained for backward compatibility.

C# 类库C# class libraries

在类库函数中,还可以使用 Disable 属性来防止函数被触发。In a class library function, you can also use the Disable attribute to prevent the function from being triggered. 可以使用不带构造函数参数的属性,如以下示例中所示:You can use the attribute without a constructor parameter, as shown in the following example:

public static class QueueFunctions
{
    [Disable]
    [FunctionName("QueueTrigger")]
    public static void QueueTrigger(
        [QueueTrigger("myqueue-items")] string myQueueItem, 
        TraceWriter log)
    {
        log.Info($"C# function processed: {myQueueItem}");
    }
}

不带构造函数参数的属性要求重新编译并重新部署项目,以更改函数的禁用状态。The attribute without a constructor parameter requires that you recompile and redeploy the project to change the function's disabled state. 该属性的更灵活用法是包含一个引用布尔应用设置的构造函数参数,如以下示例中所示:A more flexible way to use the attribute is to include a constructor parameter that refers to a Boolean app setting, as shown in the following example:

public static class QueueFunctions
{
    [Disable("MY_TIMER_DISABLED")]
    [FunctionName("QueueTrigger")]
    public static void QueueTrigger(
        [QueueTrigger("myqueue-items")] string myQueueItem, 
        TraceWriter log)
    {
        log.Info($"C# function processed: {myQueueItem}");
    }
}

使用此方法可以通过更改应用设置来启用和禁用函数,而无需重新编译或重新部署。This method lets you enable and disable the function by changing the app setting, without recompiling or redeploying. 更改应用设置会导致函数应用重启,因此,可立即识别到禁用状态的更改。Changing an app setting causes the function app to restart, so the disabled state change is recognized immediately.

重要

只能使用 Disabled 属性来禁用类库函数。The Disabled attribute is the only way to disable a class library function. 为类库函数生成的 function.json 文件不可直接编辑。The generated function.json file for a class library function is not meant to be edited directly. 如果编辑该文件,对 disabled 属性所做的任何更改都不起作用。If you edit that file, whatever you do to the disabled property will have no effect.

“管理”选项卡上的“函数状态”开关也是如此,因为它的工作方式就是更改 function.json 文件。 The same goes for the Function state switch on the Manage tab, since it works by changing the function.json file.

另请注意,门户可能指示函数已禁用,但实际上并未禁用。Also, note that the portal may indicate the function is disabled when it isn't.

Functions 1.x - 脚本语言Functions 1.x - scripting languages

在版本 1.x 中,还可以使用 function.json 文件的 disabled 属性来指示运行时不要触发函数。In version 1.x, you can also use the disabled property of the function.json file to tell the runtime not to trigger a function. 此方法仅适用于脚本语言(例如,C# 脚本和 JavaScript)。This method only works for scripting languages such as C# script and JavaScript. disabled 属性可设置为 true 或某个应用设置的名称:The disabled property can be set to true or to the name of an app setting:

{
    "bindings": [
        {
            "type": "queueTrigger",
            "direction": "in",
            "name": "myQueueItem",
            "queueName": "myqueue-items",
            "connection":"MyStorageConnectionAppSetting"
        }
    ],
    "disabled": true
}

or

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

在第二个示例中,当存在名为 IS_DISABLED 的应用设置且其值设置为 true 或 1 时,将禁用相应的函数。In the second example, the function is disabled when there is an app setting that is named IS_DISABLED and is set to true or 1.

重要

门户现在使用应用程序设置来禁用 v1.x 函数。The portal now uses application settings to disable v1.x functions. 当应用程序设置与 function.json 文件冲突时,可能会出现错误。When an application setting conflicts with the function.json file, an error can occur. 应从 function.json 文件中删除 disabled 属性,以防止出现错误。You should remove the disabled property from the function.json file to prevent errors.

后续步骤Next steps

本文介绍了如何禁用自动触发器。This article is about disabling automatic triggers. 有关触发器的详细信息,请参阅触发器和绑定For more information about triggers, see Triggers and bindings.