Azure Functions 的 Azure 事件网格输出绑定Azure Event Grid output binding for Azure Functions

使用事件网格输出绑定将事件写入到自定义主题。Use the Event Grid output binding to write events to a custom topic. 必须具有有效的自定义主题访问密钥You must have a valid access key for the custom topic.

若要了解设置和配置详细信息,请参阅概述For information on setup and configuration details, see the overview.


事件网格输出绑定不支持共享访问签名(SAS 令牌)。The Event Grid output binding does not support shared access signatures (SAS tokens). 必须使用主题的访问密钥。You must use the topic's access key.


事件网格输出绑定仅适用于 Functions 2.x 和更高版本。The Event Grid output binding is only available for Functions 2.x and higher.


以下示例演示使用方法返回值作为输出将消息写入到事件网格自定义主题的 C# 函数The following example shows a C# function that writes a message to an Event Grid custom topic, using the method return value as the output:

[return: EventGrid(TopicEndpointUri = "MyEventGridTopicUriSetting", TopicKeySetting = "MyEventGridTopicKeySetting")]
public static EventGridEvent Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
    return new EventGridEvent("message-id", "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0");

以下示例演示如何使用 IAsyncCollector 接口发送一批消息。The following example shows how to use the IAsyncCollector interface to send a batch of messages.

public static async Task Run(
    [TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
    [EventGrid(TopicEndpointUri = "MyEventGridTopicUriSetting", TopicKeySetting = "MyEventGridTopicKeySetting")]IAsyncCollector<EventGridEvent> outputEvents,
    ILogger log)
    for (var i = 0; i < 3; i++)
        var myEvent = new EventGridEvent("message-id-" + i, "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0");
        await outputEvents.AddAsync(myEvent);

特性和注释Attributes and annotations

对于 C# 类库,使用 EventGridAttribute 特性。For C# class libraries, use the EventGridAttribute attribute.

该特性的构造函数采用包含自定义主题名称的应用设置的名称,以及包含主题密钥的应用设置的名称。The attribute's constructor takes the name of an app setting that contains the name of the custom topic, and the name of an app setting that contains the topic key. 有关这些设置的详细信息,请参阅输出 - 配置For more information about these settings, see Output - configuration. 下面是 EventGrid 特性的示例:Here's an EventGrid attribute example:

[return: EventGrid(TopicEndpointUri = "MyEventGridTopicUriSetting", TopicKeySetting = "MyEventGridTopicKeySetting")]
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)

有关完整示例,请参阅示例For a complete example, see example.


下表解释了在 function.json 文件和 EventGrid 特性中设置的绑定配置属性。The following table explains the binding configuration properties that you set in the function.json file and the EventGrid attribute.

function.json 属性function.json property Attribute 属性Attribute property 说明Description
type type 不适用n/a 必须设置为“eventGrid”。Must be set to "eventGrid".
direction direction 不适用n/a 必须设置为“out”。Must be set to "out". 在 Azure 门户中创建绑定时,会自动设置该参数。This parameter is set automatically when you create the binding in the Azure portal.
name name 不适用n/a 函数代码中使用的表示事件的变量名称。The variable name used in function code that represents the event.
topicEndpointUritopicEndpointUri TopicEndpointUriTopicEndpointUri 包含自定义主题 URI 的应用设置的名称,例如 MyTopicEndpointUriThe name of an app setting that contains the URI for the custom topic, such as MyTopicEndpointUri.
topicKeySettingtopicKeySetting TopicKeySettingTopicKeySetting 包含自定义主题访问密钥的应用设置的名称。The name of an app setting that contains an access key for the custom topic.

在本地进行开发时,应用设置将取 local.settings.json 文件的值。When you're developing locally, app settings go into the local.settings.json file.


请确保将 TopicEndpointUri 配置属性的值设置为包含自定义主题 URI 的应用设置的名称。Ensure that you set the value of the TopicEndpointUri configuration property to the name of an app setting that contains the URI of the custom topic. 不要直接在此属性中指定自定义主题的 URI。Do not specify the URI of the custom topic directly in this property.


使用 out EventGridEvent paramName 等方法参数发送消息。Send messages by using a method parameter such as out EventGridEvent paramName. 若要编写多条消息,可以使用 ICollector<EventGridEvent>IAsyncCollector<EventGridEvent> 代替 out EventGridEventTo write multiple messages, you can use ICollector<EventGridEvent> or IAsyncCollector<EventGridEvent> in place of out EventGridEvent.

后续步骤Next steps