Webhooks are user defined HTTP endpoints that are usually triggered by an event. Webhooks allow us to get more out of Azure Alerts. You can specify a HTTP or HTTPS endpoint as a webhook while creating or updating an alert on the Azure Portal.
In this article I will walk you through creating an sample application to receive webhooks from Azure Alerts, configure an Alert to use this endpoint and test the overall flow.
Create a Receiver Application
Open Visual Studio 2015 and create a New ASP.Net Web Application
Select the Empty template from the available ASP.Net 4.5 Templates and Check to add the Web API folders an core references as below.
Add the Microsoft.AspNet.WebHooks.Receivers.Azure Nuget package. Don’t forget to check Include prerelease if you can find this package in the search results.
After installing the nuget package add the the below line to the Register method in WebApiConfig class.
You can add the above code after the routing code as shown in Figure 4.
This code registers your webhooks reciever.
Next step is to add the below application setting to your web.config file. This setting adds the secretkey to validate that the WebHook requests indeed Azure Alerts. It is advisable to use a SHA256 hash or similar value, which you can get from FreeFormatter Online Tools For Developers. This secret key will be part of the Reciever URL provided in the Azure Portal while creating the Azure Alerts.
<add key="MS_WebHookReceiverSecret_AzureAlert" value="d3a0f7968f7ded184194f848512c58c7f44cde25" />
Next we need to add handlers to process the webhooks data sent by Azure Alerts.
Add a new class AzureAlertsWebHooksDataHandler and add the below code to it.
This is the most basic handler. In the construct we have initialized the Reciever to handle only Azure Alert webhooks. The ExecuteAsync method is the one which is responsible for processing the data posted and return response back to indicate webhooks was received.
We will now expand this code to actually process the data received in the webhooks. Let’s store the data posted by the Azure Alerts webhooks sender in Azure table storage.
To do this first add the WindowsAzure.Storage nuget package and add the below code to import the Windows azure storage namespaces required here.
using System.Configuration; //To read connectionstring from the config files.
Also add your Azure storage connection string in the application settings as below.
<add key=”StorageConnectionString” value=”DefaultEndpointsProtocol=https;AccountName=your-account-name;AccountKey=your-account-key” />
And add the a small TableEntity implementation as below to store data in Azure table storage.
public class WHEntity : TableEntity
Finally lets modify the ExecuteAsync method to process the data send by the Webhooks sender and store it in Azure Table storage as below.
public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
// Convert to POCO type
AzureAlertNotification notification = context.GetDataOrDefault<AzureAlertNotification>();
// Get the notification name
string name = notification.Context.Name + ” — ”
WHEntity wHEntity1 = new WHEntity(this.Receiver, name);
wHEntity1.FullData = context.Data.ToString();
// Retrieve the storage account from the connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference(“azurealertdemo”);
TableOperation insertOperation = TableOperation.InsertOrReplace(wHEntity1);
The data sent in by the WebHooks sender is stored in JSON format, in the Data field of the WebHookHandlerContext object which is passed in as a parameter to the ExecuteAsync method. In the above method, I’m converting it to string and storing in Azure Table storage.
Now publish this code to an Azure Website. After publishing you can use the below URL to configure Azure Alerts to send Webhooks to the receiver we created above.
The Code in the above URL is the same as the secret key we have configured in the application settings.
Configure webhooks for Azure Alerts
Now Log in to the new Azure portal to configure an Azure alert to send Webhooks to the receiver we created above.
Browse and select a resource for which you want to configure the alerts. For simplicity lets create an alert for the above webhooks reciever Azure website we created.
Create a new alert(Webhooks currently supported on metric alerts only), and provide your webhooks reciever URL in the WebHooks field as below.
Verify the Results:
Configure the alerts help you verify the results quickly. You can accomplish this by keeping the Threshold and the Period to the minimum. I have set the Period to 5 Minutes in the above example. Hence, after 5 minutes if the threshold is reached, an alert is fired and webhooks posted to our receiver URL. This data is then processed and stored to Azure table storage as below.
Sample JSON object posted by the Azure Alerts Webhooks is as below.
“resourceRegion”: “East US”,
“portalLink”: “https://portal.azure. com/#resource/subscriptions/ <your-subscriptionId>/resourceGroups/webhooksdemo1/providers/Microsoft.Web/sites/MyWebhooksDemo1″
Alternatively you can also use the Fiddler request composer to post to you webhooks Receiver URL and check the response the corresponding updates in the Azure Table storage. Make sure that the content-type is marked as json and the request body has json similar to the above example. A fiddler request should look like the below example.
Webhooks are internally configured to retry a few times until they receive a successful response from the receiver within a short duration. Hence you might see multiple requests hitting an endpoint in the ExecuteAsync method if you are debugging it remotely.
Receive WebHooks from Azure Alerts and Kudu (Azure Web App Deployment) by Henrik F Nielsen
Introducing Microsoft ASP.NET WebHooks Preview by Henrik F Nielsen
Webhooks for Azure Alerts
How to configure webhooks for alerts