Last active
January 25, 2018 23:54
-
-
Save jeffhollan/37657ae85fb639091288793d6c38f26a to your computer and use it in GitHub Desktop.
Event Grid trigger as HTTP with validation handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Net; | |
| using System.Net.Http; | |
| using System.Threading.Tasks; | |
| using Microsoft.Azure.WebJobs; | |
| using Microsoft.Azure.WebJobs.Extensions.Http; | |
| using Microsoft.Azure.WebJobs.Host; | |
| using Newtonsoft.Json.Linq; | |
| namespace EventGridDocs | |
| { | |
| public static class HttpTrigger | |
| { | |
| [FunctionName("HttpTrigger")] | |
| public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post")]HttpRequestMessage req, TraceWriter log) | |
| { | |
| log.Info("C# HTTP trigger function processed a request."); | |
| var messages = await req.Content.ReadAsAsync<JArray>(); | |
| if (messages.Count > 0 && string.Equals((string)messages[0]["eventType"], "Microsoft.EventGrid.SubscriptionValidationEvent", System.StringComparison.OrdinalIgnoreCase)) | |
| { | |
| log.Info("Validate request received"); | |
| return req.CreateResponse<object>(new | |
| { | |
| validationResponse = messages[0]["data"]["validationCode"] | |
| }); | |
| } | |
| foreach (dynamic message in messages) | |
| { | |
| log.Info($"message type: {message["eventType"]}"); | |
| log.Info($"message data: {message["data"]}"); | |
| } | |
| return req.CreateResponse(HttpStatusCode.OK); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment