|
2 | 2 | title: Use Azure Event Grid with events in CloudEvents schema
|
3 | 3 | description: Describes how to use the CloudEvents schema for events in Azure Event Grid. The service supports events in the JSON implementation of CloudEvents.
|
4 | 4 | ms.topic: conceptual
|
5 |
| -ms.date: 07/22/2021 |
| 5 | +ms.date: 07/20/2022 |
6 | 6 | ms.devlang: csharp, javascript
|
7 | 7 | ms.custom: devx-track-js, devx-track-csharp, devx-track-azurecli, devx-track-azurepowershell
|
8 | 8 | ---
|
@@ -126,76 +126,32 @@ If you're already familiar with Event Grid, you might be aware of the endpoint v
|
126 | 126 | <a name="azure-functions"></a>
|
127 | 127 |
|
128 | 128 | ## Use with Azure Functions
|
129 |
| - |
130 |
| -The [Azure Functions Event Grid binding](../azure-functions/functions-bindings-event-grid.md) doesn't natively support CloudEvents, so HTTP-triggered functions are used to read CloudEvents messages. When you use an HTTP trigger to read CloudEvents, you have to write code for what the Event Grid trigger does automatically: |
131 |
| - |
132 |
| -* Sends a validation response to a [subscription validation request](../event-grid/webhook-event-delivery.md) |
133 |
| -* Invokes the function once per element of the event array contained in the request body |
134 |
| - |
135 |
| -For information about the URL to use for invoking the function locally or when it runs in Azure, see the [HTTP trigger binding reference documentation](../azure-functions/functions-bindings-http-webhook.md). |
136 |
| - |
137 |
| -The following sample C# code for an HTTP trigger simulates Event Grid trigger behavior. Use this example for events delivered in the CloudEvents schema. |
| 129 | +The following example shows an Azure Functions version 3.x function that uses a `CloudEvent` binding parameter and `EventGridTrigger`. |
138 | 130 |
|
139 | 131 | ```csharp
|
140 |
| -[FunctionName("HttpTrigger")] |
141 |
| -public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", "options", Route = null)]HttpRequestMessage req, ILogger log) |
| 132 | +using Azure.Messaging; |
| 133 | +using Microsoft.Azure.WebJobs; |
| 134 | +using Microsoft.Azure.WebJobs.Extensions.EventGrid; |
| 135 | +using Microsoft.Extensions.Logging; |
| 136 | + |
| 137 | +namespace Company.Function |
142 | 138 | {
|
143 |
| - log.LogInformation("C# HTTP trigger function processed a request."); |
144 |
| - if (req.Method == HttpMethod.Options) |
| 139 | + public static class CloudEventTriggerFunction |
145 | 140 | {
|
146 |
| - // If the request is for subscription validation, send back the validation code |
147 |
| - |
148 |
| - var response = req.CreateResponse(HttpStatusCode.OK); |
149 |
| - response.Headers.Add("Webhook-Allowed-Origin", "eventgrid.azure.net"); |
150 |
| - |
151 |
| - return response; |
| 141 | + [FunctionName("CloudEventTriggerFunction")] |
| 142 | + public static void Run( |
| 143 | + ILogger logger, |
| 144 | + [EventGridTrigger] CloudEvent e) |
| 145 | + { |
| 146 | + logger.LogInformation("Event received {type} {subject}", e.Type, e.Subject); |
| 147 | + } |
152 | 148 | }
|
153 |
| - |
154 |
| - var requestmessage = await req.Content.ReadAsStringAsync(); |
155 |
| - var message = JToken.Parse(requestmessage); |
156 |
| - |
157 |
| - // The request isn't for subscription validation, so it's for an event. |
158 |
| - // CloudEvents schema delivers one event at a time. |
159 |
| - log.LogInformation($"Source: {message["source"]}"); |
160 |
| - log.LogInformation($"Time: {message["eventTime"]}"); |
161 |
| - log.LogInformation($"Event data: {message["data"].ToString()}"); |
162 |
| - |
163 |
| - return req.CreateResponse(HttpStatusCode.OK); |
164 | 149 | }
|
165 | 150 | ```
|
166 | 151 |
|
167 |
| -The following sample JavaScript code for an HTTP trigger simulates Event Grid trigger behavior. Use this example for events delivered in the CloudEvents schema. |
168 |
| - |
169 |
| -```javascript |
170 |
| -module.exports = function (context, req) { |
171 |
| - context.log('JavaScript HTTP trigger function processed a request.'); |
172 |
| - |
173 |
| - if (req.method == "OPTIONS") { |
174 |
| - // If the request is for subscription validation, send back the validation code |
175 |
| - |
176 |
| - context.log('Validate request received'); |
177 |
| - context.res = { |
178 |
| - status: 200, |
179 |
| - headers: { |
180 |
| - 'Webhook-Allowed-Origin': 'eventgrid.azure.net', |
181 |
| - }, |
182 |
| - }; |
183 |
| - } |
184 |
| - else |
185 |
| - { |
186 |
| - var message = req.body; |
187 |
| - |
188 |
| - // The request isn't for subscription validation, so it's for an event. |
189 |
| - // CloudEvents schema delivers one event at a time. |
190 |
| - var event = JSON.parse(message); |
191 |
| - context.log('Source: ' + event.source); |
192 |
| - context.log('Time: ' + event.eventTime); |
193 |
| - context.log('Data: ' + JSON.stringify(event.data)); |
194 |
| - } |
195 |
| - |
196 |
| - context.done(); |
197 |
| -}; |
198 |
| -``` |
| 152 | +For more information, see [Azure Event Grid trigger for Azure Functions](../azure-functions/functions-bindings-event-grid-trigger.md?tabs=in-process%2Cextensionv3&pivots=programming-language-csharp). |
| 153 | + |
| 154 | + |
199 | 155 |
|
200 | 156 | ## Next steps
|
201 | 157 |
|
|
0 commit comments