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

Azure Functions 支持 HTTP 和现成的计时器。Azure Functions supports HTTP and timer out of the box. 若要使用其他服务,你需要安装或注册绑定扩展。To work with other services, you need to install or register a binding extension. 绑定扩展是通过 Azure Core Tools 或 NuGet 程序包提供的。Binding extensions are provided via Azure Core Tools or NuGet packages.

下表指明了何时以及如何注册绑定。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 with prompt
非 .NET 语言或本地 Azure Core Tools 开发Non-.NET languages or local Azure Core Tools development 自动Automatic 使用 Core Tools CLI 命令Use Core Tools CLI commands
使用 Visual Studio 2017 的 C# 类库C# class library using Visual Studio 2017 使用 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

以下绑定类型例外,它们不需要显式注册,因为它们会在所有版本和环境中自动注册:HTTP 和计时器。The following binding types are exceptions that don't require explicit registration because they are automatically registered in all versions and environments: HTTP and timer.

Important

本文的其余内容仅适用于 Functions 2.x。This content for the rest of this article applies only to Functions 2.x. 在 Functions 1.x 中,除了使用 Visual Studio 2017 创建 C# 类库时,绑定扩展不是显式注册的。Binding extensions aren't explicitly registered in Functions 1.x except when creating a C# class library using Visual Studio 2017.

使用 Azure 门户进行开发Azure portal development

创建函数或添加绑定时,如果触发器或绑定的扩展需要注册,则系统会显示提示。When you create a function or add a binding, you are prompted when the extension for the trigger or binding requires registration. 单击“安装”注册扩展,以响应提示。Respond to the prompt by clicking Install to register the extension. 在消耗计划中,安装最多需要 10 分钟。Installation can take up to 10 minutes on a consumption plan.

对于给定的函数应用,只需安装每个扩展一次。You need only install each extension one time for a given function app. 对于门户中不可用的受支持绑定或要更新已安装的扩展,还可以从门户手动安装或更新 Azure Functions 绑定扩展For supported bindings that are not available in the portal or to update the an installed extension, you can also manually install or update Azure Functions binding extensions from the portal.

使用 Azure Functions Core Tools 进行本地开发Local development Azure Functions Core Tools

在本地开发函数时,可以使用 Azure Functions Core Tools 从终端或从命令提示符安装所需的扩展。When you develop functions locally, you can install the extensions you need by using the Azure Functions Core Tools from the Terminal or from a command prompt.

更新 function.json 文件以包含函数所需的所有绑定后,请在项目文件夹中运行以下命令。After you have updated your function.json file to include all the bindings that your function needs, run the following command in the project folder.

func extensions install

该命令读取 function.json 文件以了解所需的程序包,安装这些包并重新生成扩展项目。The command reads the function.json file to see which packages you need, installs them, and rebuilds the extensions project. 它在当前版本中添加任何新绑定,但不更新现有绑定。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.

如要安装特定版本的包或要在编辑 function.json 文件之前安装包,请对包名称使用 func extensions install 命令,如下例所示:If you want to install a particular version of a package or you want to install packages before editing the function.json file, use the func extensions install command with the name of the package, as shown in the following example:

func extensions install --package Microsoft.Azure.WebJobs.ServiceBus --version <target_version>

<target_version> 替换为特定包版本,例如 3.0.0-beta5Replace <target_version> with a specific version of the package, such as 3.0.0-beta5. NuGet.org 上的单个包页上列出了有效版本。Valid versions are listed on the individual package pages at NuGet.org.

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

在“Visual Studio 2017”中,可使用 Install-Package 命令从包管理器控制台安装包,如以下示例所示:In Visual Studio 2017, 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 to use 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.

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

在“Visual Studio Code”中,可在 .NET Core CLI 中,通过命令提示符使用 dotnet add package 命令来安装包,如以下示例所示:In Visual Studio Code, you can install packages from the command prompt using the dotnet add package command in the .NET Core CLI, as shown in the following example:

dotnet add package Microsoft.Azure.WebJobs.Extensions.ServiceBus --version <target_version>

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

用于给定绑定的包的名称在该绑定的参考文章中提供。The name of the package to use 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.

后续步骤Next steps