您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

注册 Azure Functions 绑定扩展Register Azure Functions binding extensions

在 Azure Functions 版本 2.x 中,绑定以独立于函数运行时的包的形式提供。In Azure Functions version 2.x, bindings are available as separate packages from the functions runtime. 虽然 .NET 函数通过 NuGet 包访问绑定,但扩展捆绑包允许其他函数通过配置设置访问所有绑定。While .NET functions access bindings through NuGet packages, extension bundles allow other functions access to all bindings through a configuration setting.

请考虑以下与绑定扩展相关的项:Consider the following items related to binding extensions:

下表指明了何时以及如何注册绑定。The following table indicates when and how you register bindings.

开发环境Development environment 注册Registration
Functions 1.x 中注册in Functions 1.x
注册Registration
Functions 2.x 中注册in Functions 2.x
Azure 门户Azure portal 自动Automatic 自动Automatic
非 .NET 语言或本地 Azure Core Tools 开发Non-.NET languages or local Azure Core Tools development 自动Automatic 使用 Azure Functions Core Tools 和扩展捆绑包Use Azure Functions Core Tools and extension bundles
C#使用 Visual Studio 的类库C# class library using Visual Studio 使用 NuGet 工具Use NuGet tools 使用 NuGet 工具Use NuGet tools
使用 Visual Studio Code 的 C# 类库C# class library using Visual Studio Code 不可用N/A 使用 .NET Core CLIUse .NET Core CLI

扩展捆绑包以进行本地开发Extension bundles for local development

扩展捆绑包是可以添加一兼容的函数绑定到函数应用项目的扩展的版本 2.x 运行时的本地开发技术。Extension bundles is a local development technology for the version 2.x runtime that lets you add a compatible set of Functions binding extensions to your function app project. 然后部署包中包含这些扩展包,部署到 Azure 时。These extension packages are then included in the deployment package when you deploy to Azure. 捆绑包可以由 Microsoft 通过在设置发布的所有绑定host.json文件。Bundles makes all bindings published by Microsoft available through a setting in the host.json file. 在绑定中定义的扩展包都兼容,从而帮助您避免包之间的冲突。Extension packages defined in a bundle are compatible with each other, which helps you avoid conflicts between packages. 当开发本地,请确保使用最新版本Azure Functions Core ToolsWhen developing locally, make sure you are using the latest version of Azure Functions Core Tools.

用于所有本地开发使用 Azure Functions Core Tools 或 Visual Studio Code 扩展捆绑包。Use extension bundles for all local development using Azure Functions Core Tools or Visual Studio Code.

如果不使用扩展捆绑包,则必须安装.NET Core 2.x SDK 之前安装的任何绑定扩展在本地计算机上。If you don't use extension bundles, you must install the .NET Core 2.x SDK on your local computer before you install any binding extensions. 捆绑包中删除以进行本地开发此要求。Bundles removes this requirement for local development.

若要使用扩展捆绑包,更新host.json文件以包括以下条目extensionBundle:To use extension bundles, update the host.json file to include the following entry for extensionBundle:

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

以下属性可用于在extensionBundle:The following properties are available in extensionBundle:

属性Property 描述Description
id Microsoft Azure Functions 扩展绑定命名空间。The namespace for Microsoft Azure Functions extension bundles.
version 若要安装的包的版本。The version of the bundle to install. Functions 运行时总是选择接口定义的版本范围或间隔的最高允许版本。The Functions runtime always picks the maximum permissible version defined by the version range or interval. 上面指定的版本值允许所有捆绑包版本为 1.0.0,但不是包括 2.0.0。The version value above allows all bundle versions from 1.0.0 up to but not including 2.0.0. 有关详细信息,请参阅间隔表示法,用于指定版本范围For more information, see the interval notation for specifying version ranges.

作为捆绑包更改中的包捆绑在版本增量。Bundle versions increment as packages in the bundle change. 包捆绑中的增加的主要版本,通常与 Functions 运行时的主版本中的更改时,会发生的主版本更改。Major version changes occur when packages in the bundle increment by a major version, which usually coincides with a change in the major version of the Functions runtime.

在此枚举的默认捆绑包安装的扩展当前集extensions.json 文件The current set of extensions installed by the default bundle are enumerated in this extensions.json file.

C#使用 Visual Studio 的类库C# class library with Visual Studio

在中Visual Studio,可以从使用程序包管理器控制台安装程序包Install-package命令,如下面的示例中所示:In Visual Studio, you can install packages from the Package Manager Console using the Install-Package command, as shown in the following example:

Install-Package Microsoft.Azure.WebJobs.Extensions.ServiceBus -Version <TARGET_VERSION>

用于给定绑定的包的名称在该绑定的参考文章中提供。The name of the package used for a given binding is provided in the reference article for that binding. 有关示例,请参阅服务总线绑定参考文章的“包”部分For an example, see the Packages section of the Service Bus binding reference article.

将示例中的 <TARGET_VERSION> 替换为特定包版本,例如 3.0.0-beta5Replace <TARGET_VERSION> in the example with a specific version of the package, such as 3.0.0-beta5. NuGet.org 上的单个包页上列出了有效版本。与 Functions 运行时 1.x 或 2.x 对应的主版本在绑定的参考文章中指定。Valid versions are listed on the individual package pages at NuGet.org. The major versions that correspond to Functions runtime 1.x or 2.x are specified in the reference article for the binding.

如果您使用Install-Package若要引用一个绑定,您不必使用扩展捆绑包If you use Install-Package to reference a binding, you do not need to use extension bundles. 这种方法是特定于 Visual Studio 中构建的类库。This approach is specific for class libraries built in Visual Studio.

C#使用 Visual Studio Code 的类库C# class library with Visual Studio Code

备注

我们建议使用扩展捆绑包能够自动安装兼容的一组绑定扩展包的函数。We recommend using extension bundles to have Functions automatically install a compatible set of binding extension packages.

在中Visual Studio Code,安装的包C#从命令提示符处使用类库项目dotnet 添加包.NET Core CLI 命令。In Visual Studio Code, install packages for a C# class library project from the command prompt using the dotnet add package command in the .NET Core CLI. 下面的示例演示如何添加绑定:The following example demonstrates how you add a binding:

dotnet add package Microsoft.Azure.WebJobs.Extensions.<BINDING_TYPE_NAME> --version <TARGET_VERSION>

.NET Core CLI 只能用于 Azure Functions 2.x 开发。The .NET Core CLI can only be used for Azure Functions 2.x development.

替换为<BINDING_TYPE_NAME>与为你所需的绑定参考文章中提供的包的名称。Replace <BINDING_TYPE_NAME> with the name of the package provided in the reference article for your desired binding. 您可以找到所需的绑定参考文章中支持的绑定的列表You can find the desired binding reference article in the list of supported bindings.

将示例中的 <TARGET_VERSION> 替换为特定包版本,例如 3.0.0-beta5Replace <TARGET_VERSION> in the example with a specific version of the package, such as 3.0.0-beta5. NuGet.org 上的单个包页上列出了有效版本。与 Functions 运行时 1.x 或 2.x 对应的主版本在绑定的参考文章中指定。Valid versions are listed on the individual package pages at NuGet.org. The major versions that correspond to Functions runtime 1.x or 2.x are specified in the reference article for the binding.

后续步骤Next steps