Azure Functions 的 Azure 事件网格绑定Azure Event Grid bindings for Azure Functions

本参考介绍了如何在 Azure Functions 中处理事件网格事件。This reference explains how to handle Event Grid events in Azure Functions. 有关如何在 HTTP 终结点中处理事件网格消息的详细信息,请参阅将事件接收到 HTTP 终结点For details on how to handle Event Grid messages in an HTTP end point, see Receive events to an HTTP endpoint.

事件网格是一个 Azure 服务,它可以发送 HTTP 请求来告知发布方中发生的事件情况。 Event Grid is an Azure service that sends HTTP requests to notify you about events that happen in publishers. 发布方是发起事件的服务或资源。A publisher is the service or resource that originates the event. 例如,Azure Blob 存储帐户是发布方,而 Blob 上传或删除是事件For example, an Azure blob storage account is a publisher, and a blob upload or deletion is an event. 某些 Azure 服务原生支持向事件网格发布事件Some Azure services have built-in support for publishing events to Event Grid.

事件处理程序接收并处理事件。 Event handlers receive and process events. Azure Functions 是原生支持处理事件网格事件的多个 Azure 服务之一。Azure Functions is one of several Azure services that have built-in support for handling Event Grid events. 在本参考中,你将了解如何使用事件网格触发器在从事件网格接收到事件时调用函数,并使用输出绑定将事件发送到事件网格自定义主题In this reference, you learn to use an Event Grid trigger to invoke a function when an event is received from Event Grid, and to use the output binding to send events to an Event Grid custom topic.

如果愿意,你可以使用 HTTP 触发器处理事件网格事件;请参阅将事件接收到 HTTP 终结点If you prefer, you can use an HTTP trigger to handle Event Grid Events; see Receive events to an HTTP endpoint. 目前,在以 CloudEvents 架构传递事件时,无法为 Azure Functions 应用使用事件网格触发器。Currently, you can't use an Event Grid trigger for an Azure Functions app when the event is delivered in the CloudEvents schema. 应转而使用 HTTP 触发器。Instead, use an HTTP trigger.

操作Action 类型Type
在调度事件网格事件时运行函数Run a function when an Event Grid event is dispatched 触发器Trigger
发送事件网格事件Sends an Event Grid event 输出绑定Output binding

本参考中的代码默认使用 .NET Core 语法,该语法在 Functions 2.x 版及更高版本中使用。The code in this reference defaults to .NET Core syntax, used in Functions version 2.x and higher. 有关 1.x 语法的信息,请参阅 1.x functions 模板For information on the 1.x syntax, see the 1.x functions templates.

添加到 Functions 应用Add to your Functions app

Functions 2.x 及更高版本Functions 2.x and higher

使用触发器和绑定需要引用相应的程序包。Working with the trigger and bindings requires that you reference the appropriate package. NuGet 包用于 .NET 类库,而扩展捆绑包用于其他所有应用程序类型。The NuGet package is used for .NET class libraries while the extension bundle is used for all other application types.

语言Language 添加方式...Add by... 备注Remarks
C#C# 安装 NuGet 包版本 3.xInstalling the NuGet package, version 3.x
C# 脚本、Java、JavaScript、PowerShellC# Script, Java, JavaScript, PowerShell 注册扩展捆绑包Registering the extension bundle 建议将 Azure Tools 扩展用于 Visual Studio Code。The Azure Tools extension is recommended to use with Visual Studio Code.
C# 脚本(Azure 门户中仅限联机)C# Script (online-only in Azure portal) 添加绑定Adding a binding 若要更新现有绑定扩展而不必重新发布函数应用,请参阅更新扩展To update existing binding extensions without having to republish your function app, see Update your extensions.

Functions 1.xFunctions 1.x

Functions 1.x 应用会自动引用 Microsoft.Azure.WebJobs NuGet 程序包(版本 2.x)。Functions 1.x apps automatically have a reference the Microsoft.Azure.WebJobs NuGet package, version 2.x.

后续步骤Next steps