14

I can't figure out how to create a WebHook, using custom senders, custom handlers and a persistent sub-pub store within .Net Core 2.*.

I have read many articles and examples explaining Webhooks, but all of these articles are using .NetFramework instead of the new .Net Core.

These articles I found perticulary informative:

This is some of my current code for a WebHook Sender:

Startup.cs

public void Configure(IApplicationBuilder app, HttpConfiguration config, IHostingEnvironment env)
{
config.InitializeCustomWebHooks();
config.InitializeCustomWebHooksApis();
}

NotifyApiController.cs (WebHooks Sender)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using Aeron.WebApi.Models.WebHookDtos;
using Aeron.WebApi.WebHookProviders;
using HttpPostAttribute = Microsoft.AspNetCore.Mvc.HttpPostAttribute;

[Authorize]
public class NotifyApiController : ApiController
{
    private static readonly List<MessageDto> Messages = new List<MessageDto>();

    public List<MessageDto> Get()
    {
        return Messages;
    }

    public async Task Post(MessageDto message)
    {
        Messages.Add(message);

        await this.NotifyAsync(WebHookFilterProvider.MessagePostedEvent, new { Message = message });
        Console.WriteLine($"MessagePostedEvent Notification send for message from {message.Sender}");
    }

    public async Task Delete(long id)
    {
        var messageToDelete = Messages.FirstOrDefault(m => m.Id == id);

        Messages?.Remove(messageToDelete);

        await this.NotifyAsync(WebHookFilterProvider.MessageRemovedEvent, new { Id = id });
        Console.WriteLine($"MessageRemovedEvent Notification send for message with Id {id}");
    }
}

WebHookFilterProvider.cs

using Microsoft.AspNet.WebHooks;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

namespace Aeron.WebApi.WebHookProviders
{
    /// <summary>
    /// Use an <see cref="IWebHookFilterProvider"/> implementation to describe the events that users can 
    /// subscribe to. A wildcard filter is always registered meaning that users can register for 
    /// "all events". It is possible to have 0, 1, or more <see cref="IWebHookFilterProvider"/> 
    /// implementations.
    /// </summary>
    public class WebHookFilterProvider : IWebHookFilterProvider
    {
        public const string MessagePostedEvent = "MessagePostedEvent";
        public const string MessageRemovedEvent = "MessageRemovedEvent";

        private readonly Collection<WebHookFilter> filters = new Collection<WebHookFilter>
        {
            new WebHookFilter { Name = MessagePostedEvent, Description = "A message is posted."},
            new WebHookFilter { Name = MessageRemovedEvent, Description = "A message is removed."}
        };

        public Task<Collection<WebHookFilter>> GetFiltersAsync()
        {
            return Task.FromResult(filters);
        }
    }
}

I actually have no clue what to do from here.

Laurens Voncken
  • 215
  • 2
  • 11

0 Answers0