Creating HTTP Endpoint

Create a new .NET Core Web API project in Visual Studio

A .NET Core API project can be created in Visual Studio with the following steps –

From File menu – File >> New >> Select .NET Core under Visual C# >> Select ASP.NET Core Web Application

Provide a name the project >> Ok >> Select .NET Core 2.0 from the dropdown menu in the wizard >> Select API and >> Ok

Add a new Controller to the project

A HTTP endpoint, which is a WebHook must receive events in HTTP POST only. 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 this



The method body may look a bit confusing at this moment, but in next section it will be clarified.

Subscription Validation

A WebHook needs to be registered or subscribed to an event publisher. This WebHook that we are building here is going to receive events from Azure Event Grid. This WebHook endpoint URL needs to be subscribed to Azure Event Grid topic.

Not every HTTP endpoint can subscribe to Azure Event Grid topics. While subscribing to Azure Event Grid topic, Azure Event Grid sends a validation request to the subscribing URL endpoint with a validation code. If the endpoint echos back with the same Validation Code then only Event Grid accepts that endpoint URL as a valid endpoint for subscription.

While registering an endpoint URL to Event Grid topic it sends a message in following structure



If the registering endpoint responds (echos) back the ValidationResponse with the validationCode from subscription validation event as below then only it gets registered.



It shows URL validation failure error if the validation code can’t be echoed back from the subscriber. Line 18 to 35 takes care of the event subscription validation in above controller’s Post method.

The execution flow is something like this – Event Grid posts a subscription validation event with EventType as “eventType”: “Microsoft.EventGrid.SubscriptionValidationEvent” and validation code “validationCode”: “512d38b6-c7b8-40c8-89fe-f46f9e9622b6”.

This code checks whether the EventType is of Microsoft.EventGrid.SubscriptionValidationEvent(Line 24), if yes then it gets the ValidationCode from the request and echos back with ValidationResponse at Line 34.

Subscription to Azure Event Grid topic is onetime activity, and hence the code in if block will only be executed while subscribing the endpoint URL to the topic.

For all other event types it goes to the else block of the code. All custom event handling code needs to be in else block only.

Processing a Custom Azure Event Grid Event

An empty Azure Event Grid event schema looks like this



To prepare a custom event, CustomData needs to be injected in the EventGridEvent message schema. This contains EventType, event Id, EventTime along with Data.

CustomEvent class structure and it’s corresponding JSON payload of this sample code looks like this, using which custom event will be generated.





This custom JSON payload will be processed at the else block of the code since the EventType is of ‘MyCustomEventType’ type.

Azure Event Grid Subscription

To subscribe to Azure Event Grid topic, ASP.NET Core API project with the above controller needs to be deployed to Azure accessible location. I prefer to deploy in Azure App Services. Once deployed, the deployed URL needs to be subscribed to the Event Grid topic.

An Event Grid Topic can receive multiple events and it can have multiple subscribers too. A subscribed endpoint needs to filter evens with EventType while subscribing to Event Topic. This allows a WebHook to respond to certain events only than to respond to all types of events of a event publisher.

To subscribe to an event topic – in Azure portal Create a new Event Topic subscription, Un-check ‘Subscribe all event types’ in Event Types section. Add Event Type as ‘MyCustomEventType‘. Select Endpoint type as Web Hook and select the deployed URL (https://{}.azurewebsites.net/api/EventGridEventHandler) as Endpoint and hit the Create button.

Once subscription is done you can try out this sample code to raise a custom event to Azure Event topic and expect EventGridEventHandler WebHook to respond to it.