Skip to content

Commit e77b4ec

Browse files
Split Event Grid binding ref into separate files
1 parent 8055807 commit e77b4ec

File tree

3 files changed

+843
-802
lines changed

3 files changed

+843
-802
lines changed
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
---
2+
title: Azure Event Grid output binding for Azure Functions
3+
description: Learn to send an Event Grid event in Azure Functions.
4+
author: craigshoemaker
5+
6+
ms.topic: reference
7+
ms.date: 02/14/2020
8+
ms.author: cshoe
9+
ms.custom: fasttrack-edit
10+
---
11+
12+
# Azure Event Grid output binding for Azure Functions
13+
14+
Use the Event Grid output binding to write events to a custom topic. You must have a valid [access key for the custom topic](../event-grid/security-authentication.md#custom-topic-publishing).
15+
16+
For information on setup and configuration details, see the [overview](./functions-bindings-event-grid.md).
17+
18+
> [!NOTE]
19+
> The Event Grid output binding does not support shared access signatures (SAS tokens). You must use the topic's access key.
20+
21+
> [!IMPORTANT]
22+
> The Event Grid output binding is only available for Functions 2.x and higher.
23+
24+
# [C#](#tab/csharp)
25+
26+
The following example shows a [C# function](functions-dotnet-class-library.md) that writes a message to an Event Grid custom topic, using the method return value as the output:
27+
28+
```csharp
29+
[FunctionName("EventGridOutput")]
30+
[return: EventGrid(TopicEndpointUri = "MyEventGridTopicUriSetting", TopicKeySetting = "MyEventGridTopicKeySetting")]
31+
public static EventGridEvent Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
32+
{
33+
return new EventGridEvent("message-id", "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0");
34+
}
35+
```
36+
37+
The following example shows how to use the `IAsyncCollector` interface to send a batch of messages.
38+
39+
```csharp
40+
[FunctionName("EventGridAsyncOutput")]
41+
public static async Task Run(
42+
[TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
43+
[EventGrid(TopicEndpointUri = "MyEventGridTopicUriSetting", TopicKeySetting = "MyEventGridTopicKeySetting")]IAsyncCollector<EventGridEvent> outputEvents,
44+
ILogger log)
45+
{
46+
for (var i = 0; i < 3; i++)
47+
{
48+
var myEvent = new EventGridEvent("message-id-" + i, "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0");
49+
await outputEvents.AddAsync(myEvent);
50+
}
51+
}
52+
```
53+
54+
# [C# Script](#tab/csharp-script)
55+
56+
The following example shows the Event Grid output binding data in the *function.json* file.
57+
58+
```json
59+
{
60+
"type": "eventGrid",
61+
"name": "outputEvent",
62+
"topicEndpointUri": "MyEventGridTopicUriSetting",
63+
"topicKeySetting": "MyEventGridTopicKeySetting",
64+
"direction": "out"
65+
}
66+
```
67+
68+
Here's C# script code that creates one event:
69+
70+
```cs
71+
#r "Microsoft.Azure.EventGrid"
72+
using System;
73+
using Microsoft.Azure.EventGrid.Models;
74+
using Microsoft.Extensions.Logging;
75+
76+
public static void Run(TimerInfo myTimer, out EventGridEvent outputEvent, ILogger log)
77+
{
78+
outputEvent = new EventGridEvent("message-id", "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0");
79+
}
80+
```
81+
82+
Here's C# script code that creates multiple events:
83+
84+
```cs
85+
#r "Microsoft.Azure.EventGrid"
86+
using System;
87+
using Microsoft.Azure.EventGrid.Models;
88+
using Microsoft.Extensions.Logging;
89+
90+
public static void Run(TimerInfo myTimer, ICollector<EventGridEvent> outputEvent, ILogger log)
91+
{
92+
outputEvent.Add(new EventGridEvent("message-id-1", "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0"));
93+
outputEvent.Add(new EventGridEvent("message-id-2", "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0"));
94+
}
95+
```
96+
97+
# [JavaScript](#tab/javascript)
98+
99+
The following example shows the Event Grid output binding data in the *function.json* file.
100+
101+
```json
102+
{
103+
"type": "eventGrid",
104+
"name": "outputEvent",
105+
"topicEndpointUri": "MyEventGridTopicUriSetting",
106+
"topicKeySetting": "MyEventGridTopicKeySetting",
107+
"direction": "out"
108+
}
109+
```
110+
111+
Here's JavaScript code that creates a single event:
112+
113+
```javascript
114+
module.exports = async function (context, myTimer) {
115+
var timeStamp = new Date().toISOString();
116+
117+
context.bindings.outputEvent = {
118+
id: 'message-id',
119+
subject: 'subject-name',
120+
dataVersion: '1.0',
121+
eventType: 'event-type',
122+
data: "event-data",
123+
eventTime: timeStamp
124+
};
125+
context.done();
126+
};
127+
```
128+
129+
Here's JavaScript code that creates multiple events:
130+
131+
```javascript
132+
module.exports = function(context) {
133+
var timeStamp = new Date().toISOString();
134+
135+
context.bindings.outputEvent = [];
136+
137+
context.bindings.outputEvent.push({
138+
id: 'message-id-1',
139+
subject: 'subject-name',
140+
dataVersion: '1.0',
141+
eventType: 'event-type',
142+
data: "event-data",
143+
eventTime: timeStamp
144+
});
145+
context.bindings.outputEvent.push({
146+
id: 'message-id-2',
147+
subject: 'subject-name',
148+
dataVersion: '1.0',
149+
eventType: 'event-type',
150+
data: "event-data",
151+
eventTime: timeStamp
152+
});
153+
context.done();
154+
};
155+
```
156+
157+
# [Python](#tab/python)
158+
159+
The Event Grid output binding is not available for Python.
160+
161+
# [Java](#tab/java)
162+
163+
The Event Grid output binding is not available for Java.
164+
165+
---
166+
167+
## Attributes and annotations
168+
169+
# [C#](#tab/csharp)
170+
171+
For [C# class libraries](functions-dotnet-class-library.md), use the [EventGridAttribute](https://github.com/Azure/azure-functions-eventgrid-extension/blob/dev/src/EventGridExtension/OutputBinding/EventGridAttribute.cs) attribute.
172+
173+
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](#configuration). Here's an `EventGrid` attribute example:
174+
175+
```csharp
176+
[FunctionName("EventGridOutput")]
177+
[return: EventGrid(TopicEndpointUri = "MyEventGridTopicUriSetting", TopicKeySetting = "MyEventGridTopicKeySetting")]
178+
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
179+
{
180+
...
181+
}
182+
```
183+
184+
For a complete example, see [Output - C# example](#output).
185+
186+
# [C# Script](#tab/csharp-script)
187+
188+
Attributes are not supported by C# Script.
189+
190+
# [JavaScript](#tab/javascript)
191+
192+
Attributes are not supported by JavaScript.
193+
194+
# [Python](#tab/python)
195+
196+
The Event Grid output binding is not available for Python.
197+
198+
# [Java](#tab/java)
199+
200+
The Event Grid output binding is not available for Java.
201+
202+
---
203+
204+
## Configuration
205+
206+
The following table explains the binding configuration properties that you set in the *function.json* file and the `EventGrid` attribute.
207+
208+
|function.json property | Attribute property |Description|
209+
|---------|---------|----------------------|
210+
|**type** | n/a | Must be set to "eventGrid". |
211+
|**direction** | n/a | Must be set to "out". This parameter is set automatically when you create the binding in the Azure portal. |
212+
|**name** | n/a | The variable name used in function code that represents the event. |
213+
|**topicEndpointUri** |**TopicEndpointUri** | The name of an app setting that contains the URI for the custom topic, such as `MyTopicEndpointUri`. |
214+
|**topicKeySetting** |**TopicKeySetting** | The name of an app setting that contains an access key for the custom topic. |
215+
216+
[!INCLUDE [app settings to local.settings.json](../../includes/functions-app-settings-local.md)]
217+
218+
> [!IMPORTANT]
219+
> 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. Do not specify the URI of the custom topic directly in this property.
220+
221+
## Usage
222+
223+
# [C#](#tab/csharp)
224+
225+
Send messages by using a method parameter such as `out EventGridEvent paramName`. To write multiple messages, you can use `ICollector<EventGridEvent>` or
226+
`IAsyncCollector<EventGridEvent>` in place of `out EventGridEvent`.
227+
228+
# [C# Script](#tab/csharp-script)
229+
230+
Send messages by using a method parameter such as `out EventGridEvent paramName`. In C# script, `paramName` is the value specified in the `name` property of *function.json*. To write multiple messages, you can use `ICollector<EventGridEvent>` or
231+
`IAsyncCollector<EventGridEvent>` in place of `out EventGridEvent`.
232+
233+
# [JavaScript](#tab/javascript)
234+
235+
Access the output event by using `context.bindings.<name>` where `<name>` is the value specified in the `name` property of *function.json*.
236+
237+
# [Python](#tab/python)
238+
239+
The Event Grid output binding is not available for Python.
240+
241+
# [Java](#tab/java)
242+
243+
The Event Grid output binding is not available for Java.
244+
245+
---
246+
247+
# Next steps
248+
249+
* [Dispatch an Event Grid event](./functions-bindings-event-grid-trigger.md)

0 commit comments

Comments
 (0)