使用 Visual Studio 开发 Azure FunctionsDevelop Azure Functions using Visual Studio

使用 Visual Studio 可以开发、测试 C# 类库函数并将其部署到 Azure。Visual Studio lets you develop, test, and deploy C# class library functions to Azure. 如果这是你第一次体验 Azure Functions,请参阅 Azure Functions 简介If this experience is your first with Azure Functions, see An introduction to Azure Functions.

Visual Studio 在开发函数时具有以下优势:Visual Studio provides the following benefits when you develop your functions:

  • 在本地开发计算机上编辑、生成和运行函数。Edit, build, and run functions on your local development computer.
  • 将 Azure Functions 项目直接发布到 Azure,并根据需要创建 Azure 资源。Publish your Azure Functions project directly to Azure, and create Azure resources as needed.
  • 使用 C# 特性直接在 C# 代码中声明函数绑定。Use C# attributes to declare function bindings directly in the C# code.
  • 开发和部署预先编译的 C# 函数。Develop and deploy pre-compiled C# functions. 与基于 C# 脚本的函数相比,预先编译的函数的冷启动性能更好。Pre-complied functions provide a better cold-start performance than C# script-based functions.
  • 可以在 C# 中编写函数的代码,同时利用 Visual Studio 开发环境的所有优势。Code your functions in C# while having all of the benefits of Visual Studio development.

本文详细介绍如何使用 Visual Studio 开发 C# 类库函数并将其发布到 Azure。This article provides details about how to use Visual Studio to develop C# class library functions and publish them to Azure. 在阅读本文之前,请考虑完成 Visual Studio 的函数快速入门Before you read this article, consider completing the Functions quickstart for Visual Studio.

除非另有说明,否则将演示 Visual Studio 2019 的过程和示例。Unless otherwise noted, procedures and examples shown are for Visual Studio 2019.


  • Azure Functions Tools。Azure Functions Tools. 若要添加 Azure Functions Tools,请确保在 Visual Studio 安装中包括 Azure 开发 工作负荷。To add Azure Function Tools, include the Azure development workload in your Visual Studio installation. 从 Visual Studio 2017 开始,Azure Functions Tools 包含在 Azure 开发工作负荷中。Azure Functions Tools is available in the Azure development workload starting with Visual Studio 2017.

  • 所需的其他资源(例如 Azure 存储帐户)将在发布过程中在订阅中创建。Other resources that you need, such as an Azure Storage account, are created in your subscription during the publishing process.

  • 如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.


在 Visual Studio 2017 中,Azure 开发工作负荷会将 Azure Functions Tools 安装为单独的扩展。In Visual Studio 2017, the Azure development workload installs Azure Functions Tools as a separate extension. 更新 Visual Studio 2017 安装时,请确保使用最新版 Azure Functions Tools。When you update your Visual Studio 2017 installation, make sure that you're using the most recent version of the Azure Functions tools. 以下部分介绍如何在 Visual Studio 2017 中检查和更新(如果需要)Azure Functions Tools 扩展。The following sections show you how to check and (if needed) update your Azure Functions Tools extension in Visual Studio 2017.

如果使用的是 Visual Studio 2019,请跳过这些部分。Skip these sections if you're using Visual Studio 2019.

在 Visual Studio 2017 中检查工具版本Check your tools version in Visual Studio 2017

  1. 在“工具”菜单中,选择“扩展和更新” 。From the Tools menu, choose Extensions and Updates. 展开“已安装” > “工具”,然后选择“Azure Functions 和 Web 作业工具”。Expand Installed > Tools, and then choose Azure Functions and Web Jobs Tools.

    验证 Functions 工具版本

  2. 记下已安装 版本 并将此版本与 发行说明中列出的最新版本进行比较。Note the installed Version and compare this version with the latest version listed in the release notes.

  3. 如果你的版本较旧,可以在 Visual Studio 中更新工具,如以下部分中所示。If your version is older, update your tools in Visual Studio as shown in the following section.

在 Visual Studio 2017 中更新工具Update your tools in Visual Studio 2017

  1. 在“扩展和更新”对话框中,展开“更新” > “Visual Studio Marketplace”,依次选择“Azure Functions 和 Web 作业工具”和“更新”。In the Extensions and Updates dialog, expand Updates > Visual Studio Marketplace, choose Azure Functions and Web Jobs Tools and select Update.

    更新 Functions 工具版本

  2. 下载工具更新后,选择“关闭”,然后关闭 Visual Studio 以触发使用 VSIX 安装程序的工具更新。After the tools update is downloaded, select Close, and then close Visual Studio to trigger the tools update with VSIX Installer.

  3. 在 VSIX 安装程序中,选择“修改”以更新工具。In VSIX Installer, choose Modify to update the tools.

  4. 在更新完成后,选择“关闭”,然后重启 Visual Studio。After the update is complete, choose Close, and then restart Visual Studio.


在 Visual Studio 2019 和更高版本中,Azure Functions Tools 扩展将作为 Visual Studio 的一部分更新。In Visual Studio 2019 and later, the Azure Functions tools extension is updated as part of Visual Studio.

创建 Azure Functions 项目Create an Azure Functions project

Visual Studio 中的 Azure Functions 项目模板创建了一个项目,该项目可发布到 Azure 中的函数应用。The Azure Functions project template in Visual Studio creates a project that you can publish to a function app in Azure. 可使用函数应用将函数分组为逻辑单元,以便更轻松地管理、部署、缩放和共享资源。You can use a function app to group functions as a logical unit for easier management, deployment, scaling, and sharing of resources.

  1. 在 Visual Studio 菜单中,选择“文件” > “新建” > “项目”。From the Visual Studio menu, select File > New > Project.

  2. 在“创建新项目”中,在搜索框中输入“functions”,选择“Azure Functions”模板,然后选择“下一步”。In Create a new project, enter functions in the search box, choose the Azure Functions template, and then select Next.

  3. 在“配置新项目”中,输入项目的“项目名称”,然后选择“创建”。In Configure your new project, enter a Project name for your project, and then select Create. 函数应用名称必须可以充当 C# 命名空间,因此请勿使用下划线、连字符或任何其他的非字母数字字符。The function app name must be valid as a C# namespace, so don't use underscores, hyphens, or any other nonalphanumeric characters.

  4. 对于“创建新的 Azure Functions 应用程序”设置,请使用下表中的值:For the Create a new Azure Functions application settings, use the values in the following table:

    设置Setting Value 说明Description
    Functions 运行时Functions runtime Azure Functions v3
    (.NET Core)
    Azure Functions v3
    (.NET Core)
    此值会创建使用 Azure Functions 的版本 3.x 运行时(支持 .NET Core 3.x)的函数项目。This value creates a function project that uses the version 3.x runtime of Azure Functions, which supports .NET Core 3.x. Azure Functions 1.x 支持 .NET Framework。Azure Functions 1.x supports the .NET Framework. 有关详细信息,请参阅 Azure Functions 运行时版本概述For more information, see Azure Functions runtime versions overview.
    函数模板Function template HTTP 触发器HTTP trigger 此值会创建由 HTTP 请求触发的函数。This value creates a function triggered by an HTTP request.
    存储帐户(AzureWebJobsStorage)Storage account (AzureWebJobsStorage) 存储模拟器Storage emulator 由于 Azure 函数需要存储帐户,因此在将项目发布到 Azure 时会分配或创建一个存储帐户。Because an Azure Function requires a storage account, one is assigned or created when you publish your project to Azure. HTTP 触发器不使用 Azure 存储帐户连接字符串;所有其他触发器类型需要有效的 Azure 存储帐户连接字符串。An HTTP trigger doesn't use an Azure Storage account connection string; all other trigger types require a valid Azure Storage account connection string.
    授权级别Authorization level 匿名Anonymous 在未提供密钥的情况下,任何客户端都可以触发创建的函数。The created function can be triggered by any client without providing a key. 通过此授权设置可以轻松测试新函数。This authorization setting makes it easy to test your new function. 有关密钥和授权的详细信息,请参阅授权密钥HTTP 和 Webhook 绑定For more information about keys and authorization, see Authorization keys and HTTP and webhook bindings.

    Azure Functions 项目设置

    请确保将“授权级别”设置为“匿名”。 Make sure you set the Authorization level to Anonymous. 如果选择默认级别的函数,需要在请求中提供函数密钥才能访问函数终结点。If you choose the default level of Function, you're required to present the function key in requests to access your function endpoint.

  5. 选择“创建”以创建函数项目和 HTTP 触发器函数。Select Create to create the function project and HTTP trigger function.

创建 Azure Functions 项目后,项目模板会创建 C# 项目,安装 Microsoft.NET.Sdk.Functions NuGet 包,并设置目标框架。After you create an Azure Functions project, the project template creates a C# project, installs the Microsoft.NET.Sdk.Functions NuGet package, and sets the target framework. 新项目包含以下文件:The new project has the following files:

  • host.json:用于配置 Functions 主机。host.json: Lets you configure the Functions host. 在本地和 Azure 中运行时,都会应用这些设置。These settings apply both when running locally and in Azure. 有关详细信息,请参阅 host.json 参考For more information, see host.json reference.

  • local.settings.json:维护本地运行函数时使用的设置。local.settings.json: Maintains settings used when running functions locally. 在 Azure 中运行时不使用这些设置。These settings aren't used when running in Azure. 有关详细信息,请参阅本地设置文件For more information, see Local settings file.


    由于 local.settings.json 文件可能包含机密,因此必须将其从项目源代码管理中排除。Because the local.settings.json file can contain secrets, you must exclude it from your project source control. 请确保此文件的“复制到输出目录”设置已设置为“如果较新则复制” 。Ensure the Copy to Output Directory setting for this file is set to Copy if newer.

有关详细信息,请参阅 Functions 类库项目For more information, see Functions class library project.

本地设置文件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.

当你发布项目时,Visual Studio 不会自动上传 local.settings.json 中的设置。Visual Studio doesn't automatically upload the settings in local.settings.json when you publish the project. 为了确保这些设置也存在于 Azure 的函数应用中,请在发布项目之后上传它们。To make sure that these settings also exist in your function app in Azure, upload them after you publish your project. 有关详细信息,请参阅函数应用设置For more information, see Function app settings. 永远不会发布 ConnectionStrings 集合中的值。The values in a ConnectionStrings collection are never published.

还可以在代码中将函数应用设置值读取为环境变量。Your code can also read the function app settings values as environment variables. 有关详细信息,请参阅环境变量For more information, see Environment variables.

配置生成输出设置Configure your build output settings

生成 Azure Functions 项目时,生成工具会优化输出,以便只保留与函数运行时共享的任何程序集的一个副本。When building an Azure Functions project, the build tools optimize the output so that only one copy of any assemblies that are shared with the functions runtime are preserved. 结果可实现经过优化的生成,可最大程度地节省空间。The result is an optimized build that saves as much space as possible. 但是,当你转向任何项目程序集的较新版本时,生成工具可能不知道必须保留这些程序集。However, when you move to a more recent version of any of your project assemblies, the build tools might not know that these assemblies must be preserved. 若要确保在优化过程中保留这些程序集,可以使用项目 (.csproj) 文件中的 FunctionsPreservedDependencies 元素指定它们:To make sure that these assemblies are preserved during the optimization process, you can specify them using FunctionsPreservedDependencies elements in the project (.csproj) file:

    <FunctionsPreservedDependencies Include="Microsoft.AspNetCore.Http.dll" />
    <FunctionsPreservedDependencies Include="Microsoft.AspNetCore.Http.Extensions.dll" />
    <FunctionsPreservedDependencies Include="Microsoft.AspNetCore.Http.Features.dll" />

为本地开发配置项目Configure the project for local development

Functions 运行时在内部使用 Azure 存储帐户。The Functions runtime uses an Azure Storage account internally. 对于除 HTTP 和 Webhook 之外的所有触发器类型,请将 Values.AzureWebJobsStorage 键设置为有效的 Azure 存储帐户连接字符串。For all trigger types other than HTTP and webhooks, set the Values.AzureWebJobsStorage key to a valid Azure Storage account connection string. 函数应用还可以在项目所需的 AzureWebJobsStorage 连接设置中使用 Azure 存储模拟器Your function app can also use the Azure Storage Emulator for the AzureWebJobsStorage connection setting that's required by the project. 若要使用模拟器,请将 AzureWebJobsStorage 的值设置为 UseDevelopmentStorage=trueTo use the emulator, set the value of AzureWebJobsStorage to UseDevelopmentStorage=true. 在部署之前,请将此设置更改为实际的存储帐户连接字符串。Change this setting to an actual storage account connection string before deployment.

若要设置存储帐户连接字符串,请执行以下操作:To set the storage account connection string:

  1. 在 Visual Studio 中,选择“视图” > “Cloud Explorer”。 In Visual Studio, select View > Cloud Explorer.

  2. 在“Cloud Explorer”中,展开“存储帐户”,然后选择你的存储帐户。In Cloud Explorer, expand Storage Accounts, and then select your storage account. 在“属性”选项卡中,复制“主连接字符串”值。In the Properties tab, copy the Primary Connection String value.

  3. 在你的项目中,打开 local.settings.json 文件,并将 AzureWebJobsStorage 键的值设置为你复制的连接字符串。In your project, open the local.settings.json file and set the value of the AzureWebJobsStorage key to the connection string you copied.

  4. 重复上一步骤,将唯一键添加到函数所需的其他任何连接的 Values 数组。Repeat the previous step to add unique keys to the Values array for any other connections required by your functions.

将函数添加到项目Add a function to your project

在 C# 类库函数中,可以通过在代码中应用属性来定义函数使用的绑定。In C# class library functions, the bindings used by the function are defined by applying attributes in the code. 从提供的模板创建函数触发器时,将为你应用触发器属性。When you create your function triggers from the provided templates, the trigger attributes are applied for you.

  1. 在“解决方案资源管理器”中,右键单击你的项目节点,然后选择“添加” > “新建项”。In Solution Explorer, right-click your project node and select Add > New Item.

  2. 选择“Azure 函数”,输入类的 名称,然后选择“添加”。 Select Azure Function, enter a Name for the class, and then select Add.

  3. 选择你的触发器,设置绑定属性,然后选择“确定”。Choose your trigger, set the binding properties, and then select OK. 以下示例显示了用于创建队列存储触发器函数的设置。The following example shows the settings for creating a Queue storage trigger function.


    此触发器示例使用包含名为 QueueStorage 的键的连接字符串。This trigger example uses a connection string with a key named QueueStorage. 请在 local.settings.json 文件中定义此连接字符串设置。Define this connection string setting in the local.settings.json file.

  4. 检查新添加的类。Examine the newly added class. 你会看到一个静态 Run() 方法,它已使用 FunctionName 属性设置了属性。You see a static Run() method that's attributed with the FunctionName attribute. 该属性指示该方法是函数的入口点。This attribute indicates that the method is the entry point for the function.

    例如,以下 C# 类表示一个基本的队列存储触发器函数:For example, the following C# class represents a basic Queue storage trigger function:

    using System;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Host;
    using Microsoft.Extensions.Logging;
    namespace FunctionApp1
        public static class Function1
            public static void Run([QueueTrigger("myqueue-items", 
                Connection = "QueueStorage")]string myQueueItem, ILogger log)
                log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

已向提供给入口点方法的每个绑定参数提供了特定于绑定的属性。A binding-specific attribute is applied to each binding parameter supplied to the entry point method. 该属性采用绑定信息作为参数。The attribute takes the binding information as parameters. 在上例中,第一个参数应用了 QueueTrigger 属性,它表示一个队列存储触发器函数。In the previous example, the first parameter has a QueueTrigger attribute applied, indicating a Queue storage trigger function. 队列名称和连接字符串设置名称作为参数传递给 QueueTrigger 属性。The queue name and connection string setting name are passed as parameters to the QueueTrigger attribute. 有关详细信息,请参阅 Azure Functions 的 Azure 队列存储绑定For more information, see Azure Queue storage bindings for Azure Functions.

可以使用上述过程向函数应用项目添加更多的函数。Use the above procedure to add more functions to your function app project. 项目中的每个函数可以有不同的触发器,但每个函数的触发器必须刚好一个。Each function in the project can have a different trigger, but a function must have exactly one trigger. 有关详细信息,请参阅 Azure Functions 触发器和绑定概念For more information, see Azure Functions triggers and bindings concepts.

添加绑定Add bindings

使用触发器时,输入和输出绑定是作为绑定属性添加到函数的。As with triggers, input and output bindings are added to your function as binding attributes. 向函数添加绑定,如下所示:Add bindings to a function as follows:

  1. 确保已为本地开发配置项目Make sure you've configured the project for local development.

  2. 为特定绑定添加适当的 NuGet 扩展包。Add the appropriate NuGet extension package for the specific binding.

    有关详细信息,请参阅使用 Visual Studio 的 C# 类库For more information, see C# class library with Visual Studio. 请在有关绑定的参考文章中查找特定于绑定的 NuGet 包要求。Find the binding-specific NuGet package requirements in the reference article for the binding. 例如,可以在事件中心绑定参考文章中找到事件中心触发器的包要求。For example, find package requirements for the Event Hubs trigger in the Event Hubs binding reference article.

  3. 如果有绑定所需的应用设置,请将其添加到本地设置文件中的 Values 集合。If there are app settings that the binding needs, add them to the Values collection in the local setting file.

    函数在本地运行时将使用这些值。The function uses these values when it runs locally. 当函数在 Azure 的函数应用中运行时,它将使用函数应用设置When the function runs in the function app in Azure, it uses the function app settings.

  4. 将适当的绑定属性添加到方法签名。Add the appropriate binding attribute to the method signature. 在以下示例中,一条队列消息触发了该函数,而输出绑定则创建了一条新的队列消息,在不同的队列中使用了相同的文本。In the following example, a queue message triggers the function, and the output binding creates a new queue message with the same text in a different queue.

    public static class SimpleExampleWithOutput
        public static void Run(
            [QueueTrigger("myqueue-items-source", Connection = "AzureWebJobsStorage")] string myQueueItem, 
            [Queue("myqueue-items-destination", Connection = "AzureWebJobsStorage")] out string myQueueItemCopy,
            ILogger log)
            log.LogInformation($"CopyQueueMessage function processed: {myQueueItem}");
            myQueueItemCopy = myQueueItem;

    到队列存储的连接从 AzureWebJobsStorage 设置获取。The connection to Queue storage is obtained from the AzureWebJobsStorage setting. 有关详细信息,请参阅特定绑定的参考文章。For more information, see the reference article for the specific binding.

下表显示了 Azure Functions 运行时的主版本支持的绑定:This table shows the bindings that are supported in the major versions of the Azure Functions runtime:

类型Type 1.x1.x 2.x 及更高版本12.x and higher1 触发器Trigger 输入Input 输出Output
Blob 存储Blob storage
Azure Cosmos DBAzure Cosmos DB
事件网格Event Grid
事件中心Event Hubs
HTTP 和 WebhookHTTP & webhooks
IoT 中心IoT Hub
移动应用Mobile Apps
通知中心Notification Hubs
队列存储Queue storage
服务总线Service Bus
表存储Table storage

1 从版本 2.x 运行时开始,除了 HTTP 和 Timer 以外,所有绑定都必须注册。1 Starting with the version 2.x runtime, all bindings except HTTP and Timer must be registered. 请参阅注册绑定扩展See Register binding extensions.

2 消耗计划中不支持触发器。2 Triggers aren't supported in the Consumption plan. 需要运行时驱动的触发器Requires runtime-driven triggers.

3 仅支持 Kubernetes、IoT Edge 和其他自托管模式。3 Supported only in Kubernetes, IoT Edge, and other self-hosted modes only.

测试函数Testing functions

Azure Functions Core Tools 允许在本地开发计算机上运行 Azure Functions 项目。Azure Functions Core Tools lets you run Azure Functions project on your local development computer. 有关详细信息,请参阅使用 Azure Functions Core ToolsFor more information, see Work with Azure Functions Core Tools. 当你首次从 Visual Studio 启动某个函数时,系统会提示你安装这些工具。You're prompted to install these tools the first time you start a function from Visual Studio.

若要在 Visual Studio 中测试函数,请执行以下操作:To test your function in Visual Studio:

  1. 按 F5。Press F5. 如果系统提示,请按 Visual Studio 的请求下载和安装 Azure Functions Core (CLI) 工具。If prompted, accept the request from Visual Studio to download and install Azure Functions Core (CLI) tools. 你还需要启用防火墙例外,这样工具才能处理 HTTP 请求。You might also need to enable a firewall exception so that the tools can handle HTTP requests.

  2. 当项目正在运行时,请像测试已部署函数一样测试代码。With the project running, test your code as you would test a deployed function.

    有关详细信息,请参阅在 Azure Functions 中测试代码的策略For more information, see Strategies for testing your code in Azure Functions. 在调试模式下运行 Visual Studio 时,将会按预期命中断点。When you run Visual Studio in debug mode, breakpoints are hit as expected.

发布到 AzurePublish to Azure

从 Visual Studio 发布时,它使用以下两种部署方法之一:When you publish from Visual Studio, it uses one of two deployment methods:

使用以下步骤将项目发布到 Azure 中的函数应用。Use the following steps to publish your project to a function app in Azure.

  1. 在“解决方案资源管理器” 中,右键单击该项目并选择“发布”。In Solution Explorer, right-click the project and select Publish.

  2. 在“目标”中,选择“Azure” 。 选择 Azure 目标

  3. 在“特定目标”中,选择“Azure 函数应用(Windows)” In Specific target, select Azure Function App (Windows)

    选择 Azure 函数应用

  4. 在“函数实例”中,选择“创建新的 Azure 函数…”,然后使用下表中指定的值 :In Function Instance, select Create a new Azure Function... and then use the values specified in the following table:

    设置Setting Value 说明Description
    名称Name 全局唯一名称Globally unique name 用于唯一标识新 Function App 的名称。Name that uniquely identifies your new function app. 接受此名称或输入新名称。Accept this name or enter a new name. 有效的字符是 a-z0-9-Valid characters are: a-z, 0-9, and -.
    订阅Subscription 订阅Your subscription 要使用的 Azure 订阅。The Azure subscription to use. 接受此订阅,或从下拉列表中选择一个新订阅。Accept this subscription or select a new one from the drop-down list.
    资源组Resource group 资源组的名称Name of your resource group 要在其中创建函数应用的资源组。The resource group in which to create your function app. 从下拉列表中选择现有资源组,或者选择“新建”来创建新的资源组。Select an existing resource group from the drop-down list or choose New to create a new resource group.
    计划类型Plan Type 消耗Consumption 将项目发布到在消耗计划中运行的函数应用时,只需为函数应用的执行付费。When you publish your project to a function app that runs in a Consumption plan, you pay only for executions of your functions app. 其他托管计划会产生更高的成本。Other hosting plans incur higher costs.
    位置Location 应用服务的位置Location of the app service 在靠近你或者靠近函数访问的其他服务的 区域中选择一个 位置Choose a Location in a region near you or other services your functions access.
    Azure 存储Azure Storage 常规用途存储帐户General-purpose storage account Functions 运行时需要 Azure 存储帐户。An Azure Storage account is required by the Functions runtime. 选择“新建”即可配置常规用途存储帐户。Select New to configure a general-purpose storage account. 也可选择一个符合存储帐户要求的现有帐户。You can also choose an existing account that meets the storage account requirements.


  5. 选择“创建”,在 Azure 中创建一个函数应用及其相关资源。Select Create to create a function app and its related resources in Azure.

  6. 在“函数实例”中,确保已选中“从包文件运行” 。In the Functions instance, make sure that Run from package file is checked. 启用从包运行模式,函数应用将使用 Zip 部署进行部署。Your function app is deployed using Zip Deploy with Run-From-Package mode enabled. 建议对你的函数项目使用此部署方法,因为它可提高性能。This is the recommended deployment method for your functions project, since it results in better performance.


  7. 选择“完成”,然后在“发布”页面上选择“发布”,将包含项目文件的包部署到 Azure 中的新函数应用 。Select Finish, and on the Publish page, select Publish to deploy the package containing your project files to your new function app in Azure.

    部署完成后,Azure 中函数应用的根 URL 将显示在“发布”选项卡中。After the deployment completes the root URL of the function app in Azure is shown in the Publish tab.

  8. 在“发布”选项卡中,请选择“在 Cloud Explorer 中管理”。In the Publish tab, choose Manage in Cloud Explorer. 此操作会在 Cloud Explorer 中打开新的函数应用 Azure 资源。This opens the new function app Azure resource in Cloud Explorer.


    借助 Cloud Explorer,可使用 Visual Studio 来查看网站的内容,启动和停止函数应用,并直接浏览到 Azure 上和 Azure 门户中的函数应用资源。Cloud Explorer lets you use Visual Studio to view the contents of the site, start and stop the function app, and browse directly to function app resources on Azure and in the Azure portal.

函数应用设置Function app settings

当你发布项目时,Visual Studio 不会自动上传这些设置,因此你还必须将你在 local.settings.json 中添加的任何设置添加到 Azure 中的函数应用中。Because Visual Studio doesn't upload these settings automatically when you publish the project, any settings you add in the local.settings.json you must also add to the function app in Azure.

将所需设置上传到 Azure 中的函数应用的最简单方法是选择“管理 Azure 应用服务设置”链接(在成功发布项目后显示)。The easiest way to upload the required settings to your function app in Azure is to select the Manage Azure App Service settings link that appears after you successfully publish your project.


选择此链接将显示用于函数应用的“应用程序设置”对话框,可以在其中添加新应用程序设置或修改现有设置。Selecting this link displays the Application settings dialog for the function app, where you can add new application settings or modify existing ones.


“本地”会显示 local.settings.json 文件中的设置值,“远程”会显示 Azure 的函数应用中的当前设置值。Local displays a setting value in the local.settings.json file, and Remote displays a current setting value in the function app in Azure. 选择“添加设置”以创建新的应用设置。Choose Add setting to create a new app setting. 使用“从本地插入值”链接将设置值复制到“远程”字段。Use the Insert value from Local link to copy a setting value to the Remote field. 你选择“确定”后,挂起的更改将写入本地设置文件和函数应用。Pending changes are written to the local settings file and the function app when you select OK.


默认情况下,不会将 local.settings.json 文件签入到源代码管理中。By default, the local.settings.json file is not checked into source control. 这意味着,如果你从源代码管理中克隆本地 Functions 项目,则项目没有 local.settings.json 文件。This means that if you clone a local Functions project from source control, the project doesn't have a local.settings.json file. 在这种情况下,需要在项目根目录中手动创建 local.settings.json 文件,以便 Application Settings 对话框按预期工作。In this case, you need to manually create the local.settings.json file in the project root so that the Application settings dialog works as expected.

还可以采用以下这些其他方法之一来管理应用程序设置:You can also manage application settings in one of these other ways:

监视函数Monitoring functions

监视函数执行的建议方法是将函数应用与 Azure Application Insights 集成。The recommended way to monitor the execution of your functions is by integrating your function app with Azure Application Insights. 在 Azure 门户中创建函数应用时,默认情况下会为你完成此集成。When you create a function app in the Azure portal, this integration is done for you by default. 但是,在 Visual Studio 发布期间创建函数应用时,Azure 中的函数应用集成未完成。However, when you create your function app during Visual Studio publishing, the integration in your function app in Azure isn't done. 若要了解如何将 Application Insights 连接到函数应用,请参阅启用 Application Insights 集成To learn how to connect Application Insights to your function app, see Enable Application Insights integration.

若要了解有关使用 Application Insights 进行监视的详细信息,请参阅监视 Azure FunctionsTo learn more about monitoring using Application Insights, see Monitor Azure Functions.

后续步骤Next steps

有关 Azure Functions Core Tools 的详细信息,请参阅使用 Azure Functions Core ToolsFor more information about the Azure Functions Core Tools, see Work with Azure Functions Core Tools.

若要详细了解如何将函数开发为 .NET 类库,请参阅 Azure Functions C# 开发人员参考For more information about developing functions as .NET class libraries, see Azure Functions C# developer reference. 本文还举例说明了如何使用属性来声明 Azure Functions 支持的各种类型的绑定。This article also links to examples of how to use attributes to declare the various types of bindings supported by Azure Functions.