How to setup WebHookReceiver manually for .Net Core?

2.7k views Asked by At

I'm trying to setup the WebHookHandler to Receive Json Data for .Net Core project. I know how the Webhook works theoritically.

There is a good amount of information available for Receiving WebHooks but the sample source code. But I need an example for .Net Core?

3

There are 3 answers

1
Daniel Pl. On BEST ANSWER

.NET Core does not support WebHooks at the moment https://github.com/aspnet/WebHooks/issues/5

0
Henrik Frystyk Nielsen On

We do want to support WebHooks for ASP.NET Core but it is still in the works. In the mean time, you might be able to look at the handler code and do something similar for ASP.NET Core.

Hope this helps!

Henrik

0
Kristian Williams On

Here's a very basic example which I got work with Azure Alerts:

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ApplicationInsightsMonitor.Controllers
{
    [Produces("application/json")]
    [Route("api/alerts")]
    public class AlertController : Controller
    {
        [HttpPost]
        public async Task<ActionResult> Post([FromBody] AIPayloadModel payload)
        {
            if (payload == default(AIPayloadModel))
            {
                return NotFound();
            }

            // Save to database

            return Ok();
        }
    }

    public class AIPayloadModel
    {
        public enum AIPayloadStatus
        {
            Activated,
            Resolved
        }

        public class AIPayloadContextModel
        {
            public enum AIConditionType
            {
                Metric,
                Event
            }

            public enum AIMetricUnit
            {
                Bytes,
                BytesPerSecond,
                Count,
                CountPerSecond,
                Percent,
                Seconds
            }

            public enum AIAggregation
            {
                Average,
                Last,
                Maximum,
                Minimum,
                None,
                Total
            }

            public class AICondition
            {

                [JsonProperty("metricName")]
                public String Name { get; set; }

                [JsonProperty("metricUnit")]
                public AIMetricUnit Units { get; set; }

                [JsonProperty("metricValue")]
                public Decimal Value { get; set; }

                [JsonProperty("threshold")]
                public Decimal Threshold { get; set; }

                [JsonProperty("windowSize")]
                public TimeSpan WindowSize { get; set; }

                [JsonProperty("timeAggregation")]
                public AIAggregation Aggregation { get; set; }

                [JsonProperty("operator")]
                public String Operator { get; set; }
            }

            [JsonProperty("timestamp")]
            public DateTime Time { get; set; }

            [JsonProperty("id")]
            public String Id { get; set; }

            [JsonProperty("name")]
            public String Name { get; set; }

            [JsonProperty("description")]
            public String Description { get; set; }

            [JsonProperty("conditionType")]
            public AIConditionType ConditionType { get; set; }

            [JsonProperty("condition")]
            public AICondition Condition { get; set; }

            [JsonProperty("subscriptionId")]
            public String SubscriptionId { get; set; }

            [JsonProperty("resourceGroupName")]
            public String ResourceGroupName { get; set; }

            [JsonProperty("resourceGroupType")]
            public String ResourceGroupType { get; set; }

            [JsonProperty("resourceName")]
            public String ResourceName { get; set; }

            [JsonProperty("resourceType")]
            public String ResourceType { get; set; }

            [JsonProperty("resourceRegion")]
            public String ResourceRegion { get; set; }

            [JsonProperty("portalLink")]
            public String PortalLink { get; set; }
        }

        [JsonProperty(PropertyName = "status")]
        public AIPayloadStatus Status { get; set; }

        [JsonProperty(PropertyName = "context")]
        public AIPayloadContextModel Context { get; set; }

        [JsonProperty(PropertyName = "properties")]
        public Dictionary<String, String> Properties { get; set; } = new Dictionary<String, String>();
    }
}

The key is using [FromBody] on the parameter and having exactly the right JSON deserialization in your model.