使用 Azure Functions Core ToolsWork with Azure Functions Core Tools

使用 Azure Functions Core Tools 可以在本地计算机上通过命令提示符或终端开发和测试函数。Azure Functions Core Tools lets you develop and test your functions on your local computer from the command prompt or terminal. 本地函数可以连接到实时 Azure 服务,你可以在本地计算机上使用完整的 Functions 运行时调试函数。Your local functions can connect to live Azure services, and you can debug your functions on your local computer using the full Functions runtime. 甚至可以将函数应用部署到 Azure 订阅。You can even deploy a function app to your Azure subscription.


不要将本地开发和门户开发混合在同一函数应用中。Do not mix local development with portal development in the same function app. 从本地项目创建和发布函数时,不应尝试维护或修改门户中的项目代码。When you create and publish functions from a local project, you should not try to maintain or modify project code in the portal.

按照以下基本步骤在本地计算机上开发函数并使用 Core Tools 将其发布到 Azure:Developing functions on your local computer and publishing them to Azure using Core Tools follows these basic steps:

Core Tools 版本Core Tools versions

Azure Functions Core Tools 有三个版本。There are three versions of Azure Functions Core Tools. 使用的版本取决于本地开发环境、所选的语言以及所需的支持级别:The version you use depends on your local development environment, choice of language, and level of support required:

你只能在给定计算机上安装一个版本的核心工具。You can only install one version of Core Tools on a given computer. 除非另有说明,否则本文中的示例适用于版本 3.x。Unless otherwise noted, the examples in this article are for version 3.x.


Azure Functions Core Tools 当前依靠 Azure CLIAzure PowerShell 通过 Azure 帐户进行身份验证。Azure Functions Core Tools currently depends on either the Azure CLI or Azure PowerShell for authenticating with your Azure account. 这意味着必须安装以下工具之一才能通过 Azure Functions Core Tools 发布到 AzureThis means that you must install one of these tools to be able to publish to Azure from Azure Functions Core Tools.

安装 Azure Functions Core ToolsInstall the Azure Functions Core Tools

Azure Functions Core Tools 包含同一运行时的另一版本,该版本为本地开发计算机上可运行的 Azure Functions 运行时提供支持。Azure Functions Core Tools includes a version of the same runtime that powers Azure Functions runtime that you can run on your local development computer. 它还提供用于创建函数、连接到 Azure 和部署函数项目的命令。It also provides commands to create functions, connect to Azure, and deploy function projects.

版本 3.x 和 2.xVersion 3.x and 2.x

3.x/2.x 版工具使用构建在 .NET Core 之上的 Azure Functions 运行时。Version 3.x/2.x of the tools uses the Azure Functions runtime that is built on .NET Core. .NET Core 支持的所有平台(包括 WindowsmacOSLinux)都支持此版本。This version is supported on all platforms .NET Core supports, including Windows, macOS, and Linux.


可以使用扩展捆绑包绕过安装 .NET Core SDK 的要求。You can bypass the requirement for installing the .NET Core SDK by using extension bundles.

以下步骤使用 Windows 安装程序 (MSI) 安装 Core Tools v3.x。The following steps use a Windows installer (MSI) to install Core Tools v3.x. 若要详细了解安装 Core Tools v2.x 所需的其他基于包的安装程序,请参阅 Core Tools 自述文件For more information about other package-based installers, which are required to install Core Tools v2.x, see the Core Tools readme.

  1. 基于Windows 版本下载并运行 Core Tools 安装程序:Download and run the Core Tools installer, based on your version of Windows:

  2. 如果不打算使用扩展捆绑包,请安装用于 Windows 的 .NET Core 3.x SDKIf you don't plan to use extension bundles, install the .NET Core 3.x SDK for Windows.

创建本地 Functions 项目Create a local Functions project

Functions 项目目录包含文件 host.jsonlocal.settings.json 以及若干个子文件夹,这些子文件夹包含各个函数的代码。A Functions project directory contains the files host.json and local.settings.json, along with subfolders that contain the code for individual functions. 此目录相当于 Azure 中的一个函数应用。This directory is the equivalent of a function app in Azure. 若要详细了解 Functions 文件夹的结构,请参阅 Azure Functions 开发人员指南To learn more about the Functions folder structure, see the Azure Functions developers guide.

版本 3.x/2.x 要求在初始化项目时为项目选择默认语言。Version 3.x/2.x requires you to select a default language for your project when it is initialized. 在版本 3.x/2.x 中,添加的所有函数均使用默认语言模板。In version 3.x/2.x, all functions added use default language templates. 在版本 1.x 中,每次创建函数时都要指定语言。In version 1.x, you specify the language each time you create a function.

在终端窗口中或者在命令提示符下,运行以下命令创建项目和本地 Git 存储库:In the terminal window or from a command prompt, run the following command to create the project and local Git repository:

func init MyFunctionProj


Java 使用 Maven archetype 来创建本地 Functions 项目,以及第一个由 HTTP 触发的函数。Java uses a Maven archetype to create the local Functions project, along with your first HTTP triggered function. 使用以下命令创建 Java 项目:mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetypeUse the following command to create your Java project: mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype. 有关使用 Maven archetype 的示例,请参阅命令行快速入门For an example using the Maven archetype, see the Command line quickstart.

提供项目名称后,系统就会创建并初始化使用该名称的新文件夹,When you provide a project name, a new folder with that name is created and initialized. 否则会初始化当前文件夹。Otherwise, the current folder is initialized.
在版本 3.x/2.x 中运行命令时,必须为项目选择一个运行时。In version 3.x/2.x, when you run the command you must choose a runtime for your project.

Select a worker runtime:

使用向上/向下箭头键选择语言,然后按 Enter。Use the up/down arrow keys to choose a language, then press Enter. 如果计划开发 JavaScript 或 TypeScript 函数,请选择“节点”,然后选择语言。If you plan to develop JavaScript or TypeScript functions, choose node, and then select the language. TypeScript 具有一些其他要求TypeScript has some additional requirements.

JavaScript 项目的输出如以下示例所示:The output looks like the following example for a JavaScript project:

Select a worker runtime: node
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing C:\myfunctions\myMyFunctionProj\.vscode\extensions.json
Initialized empty Git repository in C:/myfunctions/myMyFunctionProj/.git/

func init 支持以下选项。除非另有说明,否则这些选项仅限版本 3.x/2.x:func init supports the following options, which are version 3.x/2.x-only, unless otherwise noted:

选项Option 描述Description
--csx 以 C# 脚本的形式创建 .NET 函数,这是版本 1.x 行为。Creates .NET functions as C# script, which is the version 1.x behavior. 仅对 --worker-runtime dotnet 有效。Valid only with --worker-runtime dotnet.
--docker 使用基于所选 --worker-runtime 的基础映像创建容器的 Dockerfile。Creates a Dockerfile for a container using a base image that is based on the chosen --worker-runtime. 如果打算发布到自定义 Linux 容器,请使用此选项。Use this option when you plan to publish to a custom Linux container.
--docker-only 将 Dockerfile 添加到现有项目中。Adds a Dockerfile to an existing project. 如果未在 local.settings.json 中指定或设置 worker-runtime,则会进行相应提示。Prompts for the worker-runtime if not specified or set in local.settings.json. 如果你打算将现有项目发布到自定义 Linux 容器,请使用此选项。Use this option when you plan to publish an existing project to a custom Linux container.
--force 即使项目中存在现有的文件,也要初始化该项目。Initialize the project even when there are existing files in the project. 此设置会覆盖同名的现有文件。This setting overwrites existing files with the same name. 项目文件夹中的其他文件不受影响。Other files in the project folder aren't affected.
--language 初始化特定于语言的项目。Initializes a language specific project. 当前在 --worker-runtime 设为 node 时受支持。Currently supported when --worker-runtime set to node. 选项包括 typescriptjavascriptOptions are typescript and javascript. 你也可使用 --worker-runtime javascript--worker-runtime typescriptYou can also use --worker-runtime javascript or --worker-runtime typescript.
--managed-dependencies 安装托管的依赖项。Installs managed dependencies. 目前只有 PowerShell 辅助运行时支持此功能。Currently, only the PowerShell worker runtime supports this functionality.
--source-control 控制是否创建 git 存储库。Controls whether a git repository is created. 默认不会创建存储库。By default, a repository isn't created. 如果为 true,则会创建存储库。When true, a repository is created.
--worker-runtime 设置项目的语言运行时。Sets the language runtime for the project. 支持的值为:csharpdotnetjavascriptnode (JavaScript)、powershelltypescriptSupported values are: csharp, dotnet, javascript,node (JavaScript), powershell, and typescript. 对于 Java 使用 Maven。如果未设置,则初始化期间系统会提示你选择运行时。For Java, use Maven.When not set, you're prompted to choose your runtime during initialization.


默认情况下,Core Tools 2.x 及更高版本会为 .NET 运行时创建函数应用项目作为 C# 类项目 (.csproj)。By default, version 2.x and later versions of the Core Tools create function app projects for the .NET runtime as C# class projects (.csproj). 这些 C# 项目可以与 Visual Studio 或 Visual Studio Code 结合使用,在测试期间以及发布到 Azure 时进行编译。These C# projects, which can be used with Visual Studio or Visual Studio Code, are compiled during testing and when publishing to Azure. 如果希望创建并使用在版本 1.x 和门户中创建的相同 C# 脚本 (.csx) 文件,则在创建和部署函数时必须包含 --csx 参数。If you instead want to create and work with the same C# script (.csx) files created in version 1.x and in the portal, you must include the --csx parameter when you create and deploy functions.

注册扩展Register extensions

除了 HTTP 和计时器触发器外,运行时 2.x 版及更高版本中的 Functions 绑定是以扩展包的形式实现的。With the exception of HTTP and timer triggers, Functions bindings in runtime version 2.x and higher are implemented as extension packages. HTTP 绑定和计时器触发器不需要扩展。HTTP bindings and timer triggers don't require extensions.

为了降低不同扩展包之间的不兼容性,可以使用函数,让你可以引用 host.json 项目文件中的扩展捆绑包。To reduce incompatibilities between the various extension packages, Functions lets you reference an extension bundle in your host.json project file. 如果选择不使用扩展捆绑包,则还需要在本地安装 .NET Core 2.x SDK,并在 functions 项目中维护 extensions.csproj。If you choose not to use extension bundles, you also need to install .NET Core 2.x SDK locally and maintain an extensions.csproj with your functions project.

在 2.x 版及更高版本的 Azure Functions 运行时中,必须显式注册在函数中使用的绑定类型的扩展。In version 2.x and beyond of the Azure Functions runtime, you have to explicitly register the extensions for the binding types used in your functions. 可以选择单独安装绑定扩展,也可以将扩展捆绑包引用添加到 host.json 项目文件。You can choose to install binding extensions individually, or you can add an extension bundle reference to the host.json project file. 扩展捆绑包可避免在使用多种绑定类型时出现包兼容性问题。Extension bundles removes the chance of having package compatibility issues when using multiple binding types. 建议使用此方法来注册绑定扩展。It is the recommended approach for registering binding extensions. 扩展捆绑包还无需安装 .NET Core 2.x SDK。Extension bundles also removes the requirement of installing the .NET Core 2.x SDK.

使用扩展捆绑包Use extension bundles

安装绑定扩展的最简单方法是启用扩展捆绑包The easiest way to install binding extensions is to enable extension bundles. 启用捆绑包时,会自动安装一组预定义的扩展包。When you enable bundles, a predefined set of extension packages is automatically installed.

若要启用扩展捆绑包,请打开 host.json 文件并更新其内容以匹配以下代码:To enable extension bundles, open the host.json file and update its contents to match the following code:

    "version": "2.0",
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"

若要了解更多信息,请参阅注册 Azure Functions 绑定扩展To learn more, see Register Azure Functions binding extensions. 在将绑定添加到 function.json 文件之前,应该先将扩展捆绑包添加到 host.json。You should add extension bundles to the host.json before you add bindings to the function.json file.

显式安装扩展Explicitly install extensions

如果无法使用扩展捆绑包,则可在本地使用 Azure Functions Core Tools 来安装项目所需的特定扩展包。If you aren't able to use extension bundles, you can use Azure Functions Core Tools locally to install the specific extension packages required by your project.


不能在使用扩展捆绑包的函数应用中显式安装扩展。You can't explicitly install extensions in a function app that is using extension bundles. 在显式安装扩展之前,请删除 host.json 中的 extensionBundle 部分。Remove the extensionBundle section in host.json before explicitly installing extensions.

以下各项介绍了你可能需要手动安装扩展的一些原因:The following items describe some reasons you might need to install extensions manually:

  • 需要访问捆绑包中不可用的扩展的特定版本。You need to access a specific version of an extension not available in a bundle.
  • 需要访问捆绑包中不可用的自定义扩展。You need to access a custom extension not available in a bundle.
  • 需要访问单个捆绑包中不可用的特定扩展组合。You need to access a specific combination of extensions not available in a single bundle.


若要使用 Core Tools 手动安装扩展,必须安装 .NET Core 2.x SDKTo manually install extensions by using Core Tools, you must have the .NET Core 2.x SDK installed. Azure Functions Core Tools 使用 .NET Core SDK 从 NuGet 安装扩展。The .NET Core SDK is used by Azure Functions Core Tools to install extensions from NuGet. 不需要了解 .NET 即可使用 Azure Functions 扩展。You don't need to know .NET to use Azure Functions extensions.

当你显式安装扩展时,系统会将名为 extensions.csproj 的 .NET 项目文件添加到项目的根目录。When you explicitly install extensions, a .NET project file named extensions.csproj is added to the root of your project. 此文件定义函数所需的 NuGet 包集。This file defines the set of NuGet packages required by your functions. 尽管你可以使用此文件中的 NuGet 包引用,但 Core Tools 允许你在不需手动编辑文件的情况下安装扩展。While you can work with the NuGet package references in this file, Core Tools lets you install extensions without having to manually edit the file.

可以通过多种方法使用 Core Tools 在本地项目中安装所需的扩展。There are several ways to use Core Tools to install the required extensions in your local project.

安装所有扩展Install all extensions

使用以下命令自动添加本地项目中的绑定所使用的所有扩展包:Use the following command to automatically add all extension packages used by the bindings in your local project:

func extensions install

该命令读取 function.json 文件以了解所需的程序包,安装这些包并重新生成扩展项目 (extensions.csproj)。The command reads the function.json file to see which packages you need, installs them, and rebuilds the extensions project (extensions.csproj). 它在当前版本中添加任何新绑定,但不更新现有绑定。It adds any new bindings at the current version but does not update existing bindings. 使用 --force 选项可在安装新版本时将现有绑定更新为最新版本。Use the --force option to update existing bindings to the latest version when installing new ones.

如果函数应用使用 Core Tools 无法识别的绑定,则必须手动安装特定扩展。If your function app uses bindings that Core Tools does not recognize, you must manually install the specific extension.

安装特定扩展Install a specific extension

使用以下命令安装特定版本的特定扩展包(在本例中为存储扩展):Use the following command to install a specific extension package at a specific version, in this case the Storage extension:

func extensions install --package Microsoft.Azure.WebJobs.Extensions.Storage --version 4.0.2

本地设置文件Local settings file

local.settings.json 文件存储应用设置、连接字符串和本地开发工具使用的设置。The local.settings.json file stores app settings, connection strings, and settings used by local development tools. 只有在本地运行项目时,才会使用 local.settings.json 文件中的设置。Settings in the local.settings.json file are used only when you're running projects locally. 本地设置文件的结构如下:The local settings file has this structure:

  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "<language worker>",
    "AzureWebJobsStorage": "<connection-string>",
    "AzureWebJobsDashboard": "<connection-string>",
    "MyBindingConnection": "<binding-connection-string>",
    "AzureWebJobs.HttpExample.Disabled": "true"
  "Host": {
    "LocalHttpPort": 7071,
    "CORS": "*",
    "CORSCredentials": false
  "ConnectionStrings": {
    "SQLConnectionString": "<sqlclient-connection-string>"

在本地运行项目时,支持这些设置:These settings are supported when you run projects locally:

设置Setting 说明Description
IsEncrypted 当此设置设为 true 时,所有值都使用本地计算机密钥进行加密。When this setting is set to true, all values are encrypted with a local machine key. func settings 命令配合使用。Used with func settings commands. 默认值为 falseDefault value is false. 当本地计算机上的 local.settings.json 文件中包含机密(如服务连接字符串)时,可能需要对其进行加密。You might want to encrypt the local.settings.json file on your local computer when it contains secrets, such as service connection strings. 主机在运行时会自动对设置解密。The host automatically decrypts settings when it runs. 在尝试读取本地加密设置之前,请使用 func settings decrypt 命令。Use the func settings decrypt command before trying to read locally encrypted settings.
Values 在本地运行项目时使用的应用程序设置和连接字符串的数组。Array of application settings and connection strings used when a project is running locally. 这些键值 (string-string) 对与 Azure 的函数应用中的应用程序设置相对应,例如 AzureWebJobsStorageThese key-value (string-string) pairs correspond to application settings in your function app in Azure, like AzureWebJobsStorage. 许多触发器和绑定都有一个引用连接字符串应用设置的属性,例如 Blob 存储触发器ConnectionMany triggers and bindings have a property that refers to a connection string app setting, like Connection for the Blob storage trigger. 对于此类属性,你需要一个在 Values 数组中定义的应用程序设置。For these properties, you need an application setting defined in the Values array. 在下表中查看常用设置的列表。See the subsequent table for a list of commonly used settings.
值必须是字符串,而不能是 JSON 对象或数组。Values must be strings and not JSON objects or arrays. 设置名称不能包含冒号 (:) 或双下划线 (__)。Setting names can't include a colon (:) or a double underline (__). 双下划线字符由运行时保留,并保留冒号以支持依赖项注入Double underline characters are reserved by the runtime, and the colon is reserved to support dependency injection.
Host 在本地运行项目时,本部分中的设置会自定义 Functions 主机进程。Settings in this section customize the Functions host process when you run projects locally. 这些设置独立于 host json 设置,后者在 Azure 中运行项目时也适用。These settings are separate from the host.json settings, which also apply when you run projects in Azure.
LocalHttpPort 设置运行本地 Functions 主机时使用的默认端口(func host startfunc run)。Sets the default port used when running the local Functions host (func host start and func run). --port 命令行选项优先于此设置。The --port command-line option takes precedence over this setting.
CORS 定义跨域资源共享 (CORS)可以使用的来源。Defines the origins allowed for cross-origin resource sharing (CORS). 以逗号分隔的列表提供来源,其中不含空格。Origins are supplied as a comma-separated list with no spaces. 支持通配符值 (*),它允许使用任何来源的请求。The wildcard value (*) is supported, which allows requests from any origin.
CORSCredentials 设置为 true 时,允许 withCredentials 请求。When set to true, allows withCredentials requests.
ConnectionStrings 一个集合。A collection. 不要将此集合用于函数绑定使用的连接字符串。Don't use this collection for the connection strings used by your function bindings. 此集合仅供通常从配置文件的 ConnectionStrings 节获取连接字符串的框架使用,例如实体框架This collection is used only by frameworks that typically get connection strings from the ConnectionStrings section of a configuration file, like Entity Framework. 此对象中的连接字符串添加到提供者类型为 System.Data.SqlClient 的环境中。Connection strings in this object are added to the environment with the provider type of System.Data.SqlClient. 此集合中的项不会使用其他应用设置发布到 Azure。Items in this collection aren't published to Azure with other app settings. 必须将这些值显式添加到函数应用设置的 Connection strings 集合中。You must explicitly add these values to the Connection strings collection of your function app settings. 如果要在函数代码中创建 SqlConnection,则应将连接字符串值与其他连接一起存储在门户中的应用程序设置中。If you're creating a SqlConnection in your function code, you should store the connection string value with your other connections in Application Settings in the portal.

在本地运行时,以下应用程序设置可包括在 Values 中:The following application settings can be included in the Values array when running locally:

设置Setting Values 说明Description
AzureWebJobsStorage 存储帐户连接字符串,或者Storage account connection string, or
包括 Azure 存储帐户的连接字符串。Contains the connection string for an Azure storage account. 如果使用 HTTP 之外的触发器,则是必需的。Required when using triggers other than HTTP. 有关详细信息,请查看 AzureWebJobsStorage 参考。For more information, see the AzureWebJobsStorage reference.
如果已在本地安装 Azure 存储仿真器,且已将 AzureWebJobsStorage 设置为 UseDevelopmentStorage=true,则 Core Tools 将使用此仿真器。When you have the Azure Storage Emulator installed locally and you set AzureWebJobsStorage to UseDevelopmentStorage=true, Core Tools uses the emulator. 此模拟器在开发期间非常有用,但是在部署之前,应当使用实际的存储连接进行测试。The emulator is useful during development, but you should test with an actual storage connection before deployment.
AzureWebJobs.<FUNCTION_NAME>.Disabled true|false 要在本地运行时禁用函数,请向集合添加 "AzureWebJobs.<FUNCTION_NAME>.Disabled": "true",其中 <FUNCTION_NAME> 是函数的名称。To disable a function when running locally, add "AzureWebJobs.<FUNCTION_NAME>.Disabled": "true" to the collection, where <FUNCTION_NAME> is the name of the function. 要了解详细信息,请参阅如何在 Azure Functions 中禁用函数To learn more, see How to disable functions in Azure Functions
指示 Functions 运行时的目标语言。Indicates the targeted language of the Functions runtime. 对于 Functions 运行时版本 2.x 及更高版本来说是必需的。Required for version 2.x and higher of the Functions runtime. 此设置是 Core Tools 为你的项目生成的。This setting is generated for your project by Core Tools. 要了解详细信息,请查看 FUNCTIONS_WORKER_RUNTIME 参考。To learn more, see the FUNCTIONS_WORKER_RUNTIME reference.
FUNCTIONS_WORKER_RUNTIME_VERSION ~7 指示在本地运行时使用 PowerShell 7。Indicates that PowerShell 7 be used when running locally. 如果未设置,则使用 PowerShell Core 6。If not set, then PowerShell Core 6 is used. 仅当在本地运行时才使用此设置。This setting is only used when running locally. 在 Azure 中运行时,PowerShell 运行时版本由 powerShellVersion 站点配置设置决定,后者可在门户中设置When running in Azure, the PowerShell runtime version is determined by the powerShellVersion site configuration setting, which can be set in the portal.

默认情况下,将项目发布到 Azure 时,这些设置不会自动迁移。By default, these settings are not migrated automatically when the project is published to Azure. 发布时使用 --publish-local-settings 开关确保已将这些设置添加到 Azure 中的函数应用。Use the --publish-local-settings switch when you publish to make sure these settings are added to the function app in Azure. 请注意,ConnectionStrings 中的值永远不会发布。Note that values in ConnectionStrings are never published.

还可以在代码中将函数应用设置值读取为环境变量。The function app settings values can also be read in your code as environment variables. 有关详细信息,请参阅以下特定于语言的参考主题的“环境变量”部分:For more information, see the Environment variables section of these language-specific reference topics:

如果没有为 AzureWebJobsStorage 设置有效的存储连接字符串并且没有使用模拟器,则会显示以下错误消息:When no valid storage connection string is set for AzureWebJobsStorage and the emulator isn't being used, the following error message is shown:

local.settings.json 中的 AzureWebJobsStorage 缺少值。Missing value for AzureWebJobsStorage in local.settings.json. 该值对除 HTTP 以外的所有触发器都是必需的。This is required for all triggers other than HTTP. 可运行“func azure functionapp fetch-app-settings <functionAppName>”或在 local.settings.json 中指定连接字符串。You can run 'func azure functionapp fetch-app-settings <functionAppName>' or specify a connection string in local.settings.json.

获取存储连接字符串Get your storage connection strings

即使在使用 Azure 存储仿真器进行开发时,也可能需要使用实际的存储连接进行测试。Even when using the Azure Storage Emulator for development, you may want to test with an actual storage connection. 假设已创建了存储帐户,则可以通过下列方式之一获取有效的存储连接字符串:Assuming you have already created a storage account, you can get a valid storage connection string in one of the following ways:

  • Azure 门户中,搜索并选择“存储帐户”。From the Azure portal, search for and select Storage accounts. 从 Azure 门户选择存储帐户Select Storage accounts from Azure portal

    选择你的存储帐户,在“设置”中选择“访问密钥”,然后复制其中一个 连接字符串 值。 Select your storage account, select Access keys in Settings, then copy one of the Connection string values. 从 Azure 门户复制连接字符串Copy connection string from Azure portal

  • 使用 Azure 存储资源管理器连接到你的 Azure 帐户。Use Azure Storage Explorer to connect to your Azure account. 在“资源管理器”中,展开你的订阅,展开“存储帐户”,选择你的存储帐户,然后复制主或辅助连接字符串。In the Explorer, expand your subscription, expand Storage Accounts, select your storage account, and copy the primary or secondary connection string.


  • 使用项目根目录中的核心工具通过下列命令之一从 Azure 下载连接字符串:Use Core Tools from the project root to download the connection string from Azure with one of the following commands:

    • 从现有函数应用下载所有设置:Download all settings from an existing function app:

      func azure functionapp fetch-app-settings <FunctionAppName>
    • 获取特定存储帐户的连接字符串。Get the Connection string for a specific storage account:

      func azure storage fetch-connection-string <StorageAccountName>

      如果你尚未登录到 Azure,系统会要求登录。When you aren't already signed in to Azure, you're prompted to do so. 这些命令将覆盖 local.settings.json 文件中的任何现有设置。These commands overwrite any existing settings in the local.settings.json file.

创建函数Create a function

若要创建函数,请运行以下命令:To create a function, run the following command:

func new

在版本 3.x/2.x 中运行 func new 时,系统会提示你选择采用函数应用默认语言的模板,另外还会提示你选择函数的名称。In version 3.x/2.x, when you run func new you are prompted to choose a template in the default language of your function app, then you are also prompted to choose a name for your function. 在版本 1.x 中,系统还会提示你选择语言。In version 1.x, you are also prompted to choose the language.

Select a language: Select a template:
Blob trigger
Cosmos DB trigger
Event Grid trigger
HTTP trigger
Queue trigger
Service Bus Queue trigger
Service Bus Topic trigger
Timer trigger

函数代码在具有所提供的函数名称的子文件夹中生成,如以下队列触发器输出中所示:Function code is generated in a subfolder with the provided function name, as you can see in the following queue trigger output:

Select a language: Select a template: Queue trigger
Function name: [QueueTriggerJS] MyQueueTrigger
Writing C:\myfunctions\myMyFunctionProj\MyQueueTrigger\index.js
Writing C:\myfunctions\myMyFunctionProj\MyQueueTrigger\readme.md
Writing C:\myfunctions\myMyFunctionProj\MyQueueTrigger\sample.dat
Writing C:\myfunctions\myMyFunctionProj\MyQueueTrigger\function.json

也可以在命令中使用以下参数指定这些选项:You can also specify these options in the command using the following arguments:

参数Argument 描述Description
--csx (2.x 及更高版本。)生成版本 1.x 和门户所用的相同 C# 脚本 (.csx) 模板。(Version 2.x and later versions.) Generates the same C# script (.csx) templates used in version 1.x and in the portal.
--language, -l--language, -l C#、F# 或 JavaScript 等模板编程语言。The template programming language, such as C#, F#, or JavaScript. 此选项在版本 1.x 中是必需的。This option is required in version 1.x. 在 2.x 及更高版本中,请不要使用此选项或选择与辅助角色运行时匹配的语言。In version 2.x and later versions, do not use this option or choose a language that matches the worker runtime.
--name, -n--name, -n 函数名称。The function name.
--template, -t--template, -t 使用 func templates list 命令查看每种受支持语言的可用模板的完整列表。Use the func templates list command to see the complete list of available templates for each supported language.

例如,若要在单个命令中创建 JavaScript HTTP 触发器,请运行:For example, to create a JavaScript HTTP trigger in a single command, run:

func new --template "Http Trigger" --name MyHttpTrigger

若要在单个命令中创建队列触发的函数,请运行:To create a queue-triggered function in a single command, run:

func new --template "Queue Trigger" --name QueueTriggerJS

在本地运行函数Run functions locally

若要运行 Functions 项目,请运行 Functions 主机。To run a Functions project, run the Functions host. 主机会为项目中的所有函数启用触发器。The host enables triggers for all functions in the project. 启动命令因项目语言而异。The start command varies, depending on your project language.

func start --build


1.x 版的 Functions 运行时需要 host 命令,如下例所示:Version 1.x of the Functions runtime requires the host command, as in the following example:

func host start

func start 支持以下选项:func start supports the following options:

选项Option 说明Description
--no-build 在运行之前请勿生成当前项目。Do no build current project before running. 仅限于 dotnet 项目。For dotnet projects only. 默认设置为 false。Default is set to false. 版本 1.x 不支持。Not supported for version 1.x.
--cors-credentials 允许跨域身份验证的请求(即 cookie 和 Authentication 标头),版本 1.x 不支持。Allow cross-origin authenticated requests (i.e. cookies and the Authentication header) Not supported for version 1.x.
--cors 以逗号分隔的 CORS 来源列表,其中不包含空格。A comma-separated list of CORS origins, with no spaces.
--language-worker 用于配置语言辅助角色的参数。Arguments to configure the language worker. 例如,可以通过提供调试端口和其他所需参数,为语言辅助角色启用调试。For example, you may enable debugging for language worker by providing debug port and other required arguments. 版本 1.x 不支持。Not supported for version 1.x.
--cert 包含私钥的 .pfx 文件的路径。The path to a .pfx file that contains a private key. 只能与 --useHttps 配合使用。Only used with --useHttps. 版本 1.x 不支持。Not supported for version 1.x.
--password 密码或包含 .pfx 文件密码的文件。Either the password or a file that contains the password for a .pfx file. 只能与 --cert 配合使用。Only used with --cert. 版本 1.x 不支持。Not supported for version 1.x.
--port, -p--port, -p 要侦听的本地端口。The local port to listen on. 默认值:7071。Default value: 7071.
--pause-on-error 退出进程前,暂停增加其他输入。Pause for additional input before exiting the process. 仅当从集成开发环境 (IDE) 启动 Core Tools 时才使用。Used only when launching Core Tools from an integrated development environment (IDE).
--script-root, --prefix--script-root, --prefix 用于指定要运行或部署的函数应用的根目录路径。Used to specify the path to the root of the function app that is to be run or deployed. 此选项用于可在子文件夹中生成项目文件的已编译项目。This is used for compiled projects that generate project files into a subfolder. 例如,生成 C# 类库项目时,将在某个根子文件夹中生成 host.json、local.settings.json 和 function.json 文件,其路径类似于 MyProject/bin/Debug/netstandard2.0For example, when you build a C# class library project, the host.json, local.settings.json, and function.json files are generated in a root subfolder with a path like MyProject/bin/Debug/netstandard2.0. 在这种情况下,请将前缀设置为 --script-root MyProject/bin/Debug/netstandard2.0In this case, set the prefix as --script-root MyProject/bin/Debug/netstandard2.0. 这是在 Azure 中运行的函数应用的根目录。This is the root of the function app when running in Azure.
--timeout, -t--timeout, -t Functions 主机启动的超时时间(以秒为单位)。The timeout for the Functions host to start, in seconds. 默认值:20 秒。Default: 20 seconds.
--useHttps 绑定到 https://localhost:{port} ,而不是绑定到 http://localhost:{port}Bind to https://localhost:{port} rather than to http://localhost:{port}. 默认情况下,此选项会在计算机上创建可信证书。By default, this option creates a trusted certificate on your computer.

Functions 主机启动时,会输出 HTTP 触发的函数的 URL:When the Functions host starts, it outputs the URL of HTTP-triggered functions:

Found the following functions:

Job host started
Http Function MyHttpTrigger: http://localhost:7071/api/MyHttpTrigger


在本地运行时,不会对 HTTP 终结点强制执行授权操作。When running locally, authorization isn't enforced for HTTP endpoints. 这意味着所有本地 HTTP 请求都将作为 authLevel = "anonymous" 处理。This means that all local HTTP requests are handled as authLevel = "anonymous". 有关详细信息,请参阅 HTTP 绑定一文。For more information, see the HTTP binding article.

将测试数据传递给函数Passing test data to a function

若要在本地测试函数,请启动 Functions 主机,并在本地服务器上使用 HTTP 请求调用终结点。To test your functions locally, you start the Functions host and call endpoints on the local server using HTTP requests. 你调用的终结点要取决于函数的类型。The endpoint you call depends on the type of function.


本主题中的示例使用 cURL 工具从终端或命令提示符发送 HTTP 请求。Examples in this topic use the cURL tool to send HTTP requests from the terminal or a command prompt. 你可以使用所选的工具将 HTTP 请求发送到本地服务器。You can use a tool of your choice to send HTTP requests to the local server. 默认情况下,cURL 工具在基于 Linux 的系统和 Windows 10 内部版本 17063 及更高版本上可用。The cURL tool is available by default on Linux-based systems and Windows 10 build 17063 and later. 在较旧的 Windows 上,必须先下载并安装 cURL 工具On older Windows, you must first download and install the cURL tool.

有关测试函数的更多常规信息,请参阅在 Azure Functions 中测试代码的策略For more general information on testing functions, see Strategies for testing your code in Azure Functions.

HTTP 和 webhook 触发的函数HTTP and webhook triggered functions

调用以下终结点,以在本地运行 HTTP 和 webhook 触发的函数:You call the following endpoint to locally run HTTP and webhook triggered functions:


请确保使用相同的服务器名称和 Functions 主机正在侦听的端口。Make sure to use the same server name and port that the Functions host is listening on. 在启动 Function 主机时所生成的输出中可以看到该信息。You see this in the output generated when starting the Function host. 可以使用触发器所支持的任何 HTTP 方法来调用此 URL。You can call this URL using any HTTP method supported by the trigger.

以下 cURL 命令使用查询字符串中传递的 name 参数从 GET 请求触发 MyHttpTrigger quickstart 函数。The following cURL command triggers the MyHttpTrigger quickstart function from a GET request with the name parameter passed in the query string.

curl --get http://localhost:7071/api/MyHttpTrigger?name=Azure%20Rocks

下面的示例是在请求主体中传递 name 的 POST 请求中调用的相同函数:The following example is the same function called from a POST request passing name in the request body:

curl --request POST http://localhost:7071/api/MyHttpTrigger --data '{"name":"Azure Rocks"}'

可以从在查询字符串中传递数据的浏览器发出 GET 请求。You can make GET requests from a browser passing data in the query string. 对于所有其他 HTTP 方法,必须使用 cURL、Fiddler、Postman 或类似的 HTTP 测试工具。For all other HTTP methods, you must use cURL, Fiddler, Postman, or a similar HTTP testing tool.

非 HTTP 触发的函数Non-HTTP triggered functions

对于 HTTP 触发器、Webhook 和事件网格触发器以外的所有类型函数,你可以通过调用管理终结点在本地测试函数。For all kinds of functions other than HTTP triggers and webhooks and Event Grid triggers, you can test your functions locally by calling an administration endpoint. 在本地服务器上通过 HTTP POST 请求调用此终结点会触发该函数。Calling this endpoint with an HTTP POST request on the local server triggers the function.

若要在本地测试事件网格触发函数,请参阅使用查看器 Web 应用进行本地测试To test Event Grid triggered functions locally, see Local testing with viewer web app.

可以选择通过 POST 请求正文将测试数据传递给执行。You can optionally pass test data to the execution in the body of the POST request. 此功能类似于 Azure 门户中的“测试”选项卡。This functionality is similar to the Test tab in the Azure portal.

可以调用以下管理员终结点以触发非 HTTP 函数:You call the following administrator endpoint to trigger non-HTTP functions:


若要将测试数据传递给函数的管理员终结点,必须在 POST 请求消息的正文中提供数据。To pass test data to the administrator endpoint of a function, you must supply the data in the body of a POST request message. 消息正文需要具有以下 JSON 格式:The message body is required to have the following JSON format:

    "input": "<trigger_input>"

<trigger_input> 值包含函数所需格式的数据。The <trigger_input> value contains data in a format expected by the function. 下面的 cURL 示例是指向 QueueTriggerJS 函数的 POST。The following cURL example is a POST to a QueueTriggerJS function. 在这种情况下,输入是一个字符串,等同于期望在队列中找到的消息。In this case, the input is a string that is equivalent to the message expected to be found in the queue.

curl --request POST -H "Content-Type:application/json" --data '{"input":"sample queue data"}' http://localhost:7071/admin/functions/QueueTrigger

使用 func run 命令(仅限版本 1.x)Using the func run command (version 1.x only)


func run 命令仅在该工具的 1.x 版中受支持。The func run command is only supported in version 1.x of the tools. 有关详细信息,请参阅主题如何指向 Azure Functions 运行时版本For more information, see the topic How to target Azure Functions runtime versions.

在版本 1.x 中,也可以使用 func run <FunctionName> 直接调用函数并为函数提供输入数据。In version 1.x, you can also invoke a function directly by using func run <FunctionName> and provide input data for the function. 此命令类似于在 Azure 门户中使用“测试”选项卡运行函数。This command is similar to running a function using the Test tab in the Azure portal.

func run 支持以下选项:func run supports the following options:

选项Option 说明Description
--content, -c--content, -c 内联内容。Inline content.
--debug, -d--debug, -d 运行函数前,将调试程序附加到主机进程。Attach a debugger to the host process before running the function.
--timeout, -t--timeout, -t 本地 Functions 主机准备就绪前的等待时间(以秒为单位)。Time to wait (in seconds) until the local Functions host is ready.
--file, -f--file, -f 要用作内容的文件名。The file name to use as content.
--no-interactive 不提示输入。Does not prompt for input. 适用于自动化方案。Useful for automation scenarios.

例如,若要调用 HTTP 触发的函数并传递内容正文,请运行以下命令:For example, to call an HTTP-triggered function and pass content body, run the following command:

func run MyHttpTrigger -c '{\"name\": \"Azure\"}'

发布到 AzurePublish to Azure

Azure Functions Core Tools 支持两种类型的部署:通过 Zip Deploy 将函数项目文件直接部署到函数应用,以及部署自定义 Docker 容器The Azure Functions Core Tools supports two types of deployment: deploying function project files directly to your function app via Zip Deploy and deploying a custom Docker container. 必须已在 Azure 订阅中创建了一个函数应用,你将向其部署代码。You must have already created a function app in your Azure subscription, to which you'll deploy your code. 应该生成需要编译的项目,以便部署二进制文件。Projects that require compilation should be built so that the binaries can be deployed.


必须在本地安装 Azure CLIAzure PowerShell,才能通过 Core Tools 发布到 Azure。You must have the Azure CLI or Azure PowerShell installed locally to be able to publish to Azure from Core Tools.

项目文件夹可能包含不应该发布的特定于语言的文件和目录。A project folder may contain language-specific files and directories that shouldn't be published. 排除的项在根项目文件夹的 .funcignore 文件中列出。Excluded items are listed in a .funcignore file in the root project folder.

部署项目文件Deploy project files

若要将本地代码发布到 Azure 中的函数应用,请使用 publish 命令:To publish your local code to a function app in Azure, use the publish command:

func azure functionapp publish <FunctionAppName>


Java 使用 Maven 将本地项目发布到 Azure。Java uses Maven to publish your local project to Azure. 使用以下命令发布到 Azure:mvn azure-functions:deployUse the following command to publish to Azure: mvn azure-functions:deploy. Azure 资源是在初始部署期间创建的。Azure resources are created during initial deployment.

此命令发布到 Azure 中的现有函数应用。This command publishes to an existing function app in Azure. 如果尝试发布到订阅中不存在的 <FunctionAppName>,则会收到错误。You'll get an error if you try to publish to a <FunctionAppName> that doesn't exist in your subscription. 若要了解如何使用 Azure CLI 或 Azure PowerShell 从命令提示符或终端窗口创建函数应用,请参阅为无服务器执行创建函数应用To learn how to create a function app from the command prompt or terminal window using the Azure CLI or Azure PowerShell, see Create a Function App for serverless execution. 默认情况下,此命令使用远程生成并将应用部署为从部署包运行By default, this command uses remote build and deploys your app to run from the deployment package. 若要禁用此建议的部署模式,请使用 --nozip 选项。To disable this recommended deployment mode, use the --nozip option.


在 Azure 门户中创建函数应用时,该应用默认使用 3.x 版函数运行时。When you create a function app in the Azure portal, it uses version 3.x of the Function runtime by default. 若要让函数应用使用 1.x 版运行时,请遵照在版本 1.x 上运行中的说明操作。To make the function app use version 1.x of the runtime, follow the instructions in Run on version 1.x. 无法为包含现有函数的函数应用更改运行时版本。You can't change the runtime version for a function app that has existing functions.

以下发布选项适用于所有版本:The following publish options apply for all versions:

选项Option 说明Description
--publish-local-settings -i 将 local.settings.json 中的设置发布到 Azure,如果该设置已存在,则提示进行覆盖。Publish settings in local.settings.json to Azure, prompting to overwrite if the setting already exists. 如果使用的是 Azure 存储仿真器,请先将应用设置更改为实际的存储连接If you are using the Azure Storage Emulator, first change the app setting to an actual storage connection.
--overwrite-settings -y 使用 --publish-local-settings -i 时隐藏覆盖应用设置的提示。Suppress the prompt to overwrite app settings when --publish-local-settings -i is used.

以下发布选项仅在 2.x 及更高版本中受支持:The following publish options are supported only for version 2.x and later versions:

选项Option 说明Description
--publish-settings-only, -o--publish-settings-only, -o 仅发布设置,并跳过内容。Only publish settings and skip the content. 默认为提示。Default is prompt.
--list-ignored-files 基于 .funcignore 文件显示发布期间忽略的文件列表。Displays a list of files that are ignored during publishing, which is based on the .funcignore file.
--list-included-files 基于 .funcignore 文件显示发布的文件列表。Displays a list of files that are published, which is based on the .funcignore file.
--nozip 关闭默认的 Run-From-Package 模式。Turns the default Run-From-Package mode off.
--build, -b--build, -b 部署到 Linux 函数应用时执行生成操作。Performs build action when deploying to a Linux function app. 接受:remotelocalAccepts: remote and local.
--additional-packages 构建本机依赖项时要安装的包列表。List of packages to install when building native dependencies.
--force 在某些情况下会忽略预发布验证。Ignore pre-publishing verification in certain scenarios.
--csx 发布 C# 脚本 (.csx) 项目。Publish a C# script (.csx) project.
--no-build 发布过程中不生成项目。Project isn't built during publishing.
--dotnet-cli-params 发布编译的 C# (.csproj) 函数时,Core Tools 将调用“dotnet build --output bin/publish”。When publishing compiled C# (.csproj) functions, the core tools calls 'dotnet build --output bin/publish'. 传递到此选项的任何参数将追加到命令行。Any parameters passed to this will be appended to the command line.

部署自定义容器Deploy custom container

Azure Functions 可让你在自定义 Docker 容器中部署函数项目。Azure Functions lets you deploy your function project in a custom Docker container. 自定义容器必须有一个 Dockerfile。Custom containers must have a Dockerfile. 若要使用 Dockerfile 创建应用,请在 func init 中使用 -dockerfile 选项。To create an app with a Dockerfile, use the --dockerfile option on func init.

func deploy

可使用以下自定义容器部署选项:The following custom container deployment options are available:

选项Option 说明Description
--registry 当前用户登录到的 Docker 注册表的名称。The name of a Docker Registry the current user signed-in to.
--platform 函数应用的托管平台。Hosting platform for the function app. 有效选项为 kubernetesValid options are kubernetes
--name 函数应用名称。Function app name.
--max (可选)设置要部署到的最大函数应用实例数。Optionally, sets the maximum number of function app instances to deploy to.
--min (可选)设置要部署到的最小函数应用实例数。Optionally, sets the minimum number of function app instances to deploy to.
--config 设置可选的部署配置文件。Sets an optional deployment configuration file.

监视函数Monitoring functions

监视函数执行的建议方法是与 Azure Application Insights 集成。The recommended way to monitor the execution of your functions is by integrating with Azure Application Insights. 还可以将执行日志流式传输到本地计算机。You can also stream execution logs to your local computer. 若要了解详细信息,请参阅监视 Azure FunctionsTo learn more, see Monitor Azure Functions.

Application Insights 集成Application Insights integration

在 Azure 中创建函数应用时,应启用 Application Insights 集成。Application Insights integration should be enabled when you create your function app in Azure. 如果由于某种原因,函数应用未连接到 Application Insights 实例,则在 Azure 门户中可以轻松地进行此集成。If for some reason your function app isn't connected to an Application Insights instance, it's easy to do this integration in the Azure portal. 若要了解详细信息,请参阅启用 Application Insights 集成To learn more, see Enable Application Insights integration.

启用流式传输日志Enable streaming logs

可以在本地计算机的命令行会话中查看函数正在生成的日志文件流。You can view a stream of log files being generated by your functions in a command-line session on your local computer.

内置日志流式处理Built-in log streaming

使用 logstream 选项开始接收在 Azure 中运行的特定函数应用的流式处理日志,如以下示例所示:Use the logstream option to start receiving streaming logs of a specific function app running in Azure, as in the following example:

func azure functionapp logstream <FunctionAppName>

实时指标流Live Metrics Stream

通过包含 --browser 选项,可在新的浏览器窗口中查看函数应用的实时指标流,如下例所示:You can view the Live Metrics Stream for your function app in a new browser window by including the --browser option, as in the following example:

func azure functionapp logstream <FunctionAppName> --browser

这种类型的流式传输日志需要为函数应用启用了 Application Insights 集成。This type of streaming logs requires that Application Insights integration be enabled for your function app.

后续步骤Next steps

了解如何使用 Azure Functions Core Tools 开发、测试和发布 Azure Functions Microsoft 学习模块 Azure Functions Core Tools 是开源工具,托管在 GitHub 上Learn how to develop, test, and publish Azure Functions by using Azure Functions Core Tools Microsoft learn module Azure Functions Core Tools is open source and hosted on GitHub.
若要提交 bug 或功能请求,请打开 GitHub 问题To file a bug or feature request, open a GitHub issue.