Skip to content

Instantly share code, notes, and snippets.

@jeffhollan
Last active January 25, 2018 23:54
Show Gist options
  • Select an option

  • Save jeffhollan/37657ae85fb639091288793d6c38f26a to your computer and use it in GitHub Desktop.

Select an option

Save jeffhollan/37657ae85fb639091288793d6c38f26a to your computer and use it in GitHub Desktop.
Event Grid trigger as HTTP with validation handling
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