Handle Events from Azure Event Grid
    • Dark
      Light
    • PDF

    Handle Events from Azure Event Grid

    • Dark
      Light
    • PDF

    Article summary

    #ServerlessTips - Azure Event Grid
    Author: Steef-Jan Wiggers Azure MVP

    Azure Event Grid is a service in Azure, allowing you to manage events centrally, and intelligently route them to one or multiple subscribers leveraging filtering capabilities it offers. Furthermore, you can send custom events or handle events using .NET code.

    Microsoft provides a NuGet package for Event Grid allowing you to send events to Azure Event Grid and to consume events delivered by Event Grid.

    It also defines the event schemas for the events published to Event Grid by various Azure services.

    In the Azure Tip on Sending Custom Events, we discussed how to send custom events to Event Grid. However, you can also consume events from Event Grid (custom or not) using the library Microsoft offers. For instance, you can create an HTTP Endpoint by creating a new .NET Core Web API project in Visual Studio; from the File menu – File -> New -> Select .NET Core under Visual C# -> Select ASP.NET Core Web Application. Next, name the project -> Ok -> Select .NET Core 2.0 from the dropdown menu in the wizard -> Select API and -> Ok.

    event-handler

    Once your project is up, you can add the NuGet package to the project. Next, you can add a new Controller to the project. Your HTTP endpoint, which is a WebHook, must receive events in HTTP POST only. Therefore, we need to create a new controller with a POST method to receive events.

    In this example, the controller name is EventGridEventHandlerController, and the Post method would look like the code below:

    using Microsoft.AspNetCore.Mvc;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Linq;
    using System.Net.Http;
    
    namespace EventGridHTTPEndpoint.Controllers
    {
        [Produces("application/json")]
        public class EventGridEventHandlerController : Controller
        {
            [HttpPost]
            [Route("api/EventGridEventHandler")]
            public JObject Post([FromBody]object request)
            {
                var eventGridEvent = JsonConvert.DeserializeObject<EventGridEvent[]>(request.ToString())
                    .FirstOrDefault();
                if (eventGridEvent == null) return new JObject(new HttpResponseMessage(System.Net.HttpStatusCode.OK));
                var data = eventGridEvent.Data as JObject;
    
                if (string.Equals(eventGridEvent.EventType, "Microsoft.EventGrid.SubscriptionValidationEvent", StringComparison.OrdinalIgnoreCase))
                {
                    if (data != null)
                    {
                        var eventData = data.ToObject<SubscriptionValidationEventData>();
                        var responseData = new SubscriptionValidationResponseData
                        {
                            ValidationResponse = eventData.ValidationCode
                        };
    
                        if (responseData.ValidationResponse != null)
                        {
                            return JObject.FromObject(responseData);
                        }
                    }
                }
                else
                {
                    if (data == null) return new JObject(new HttpResponseMessage(System.Net.HttpStatusCode.OK));
                    var eventData = data.ToObject<CustomData>();
                    var customEvent = CustomEvent<CustomData>.CreateCustomEvent(eventData);
                    return JObject.FromObject(customEvent);
                }
    
                return new JObject(new HttpResponseMessage(System.Net.HttpStatusCode.OK));
            }
        }
    }
    
    
    Note: A WebHook needs to be registered or subscribed to an event publisher; thus, the endpoint URL subscribes to Azure Event Grid topic.

    Azure Event Grid


    Was this article helpful?

    ESC

    Eddy AI, facilitating knowledge discovery through conversational intelligence