0

Is it possible to create a Webhook receiver which listens to a Azure Web Job notification whenever some record gets inserted in database (monitored by Web Job)? Once the notification is received by the Web hook, the UI needs to present a notification to the user.

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • Are you trying to make the connection between the database and the Web Job or between the Web Job and the UI? – PerfectlyPanda Feb 21 '19 at 16:09
  • There needed to be two levels of connections: One between the Web Job and database which i got working easily. The other from Web Job to the MVC UI whenever any notification gets generated from the Web Job on a db insert. I got this all working by creating a Web Api in the MVC app and sending a Post request from the Web Job to it whenever a record got inserted in the Database. Also, i used SignalR to listen to a specific event which i invoked in the Web Api code whenever a call comes to it and updated the UI in that event. – Sumit Saini Feb 22 '19 at 17:23
  • I will provide a detailed answer with a sample and a better alternative to achieve this type of scenario too. – Sumit Saini Feb 22 '19 at 17:34
  • Awesome! I was going to suggest SignalR but I wasn't quite sure where you were stuck. I'm glad you found a solution. – PerfectlyPanda Feb 22 '19 at 18:06
  • If you have found the solution, you could post your answer and mark it. – George Chen Feb 26 '19 at 05:39

1 Answers1

0

Since Sumit Saini has not posted their code, I'll add the code I wrote for a conference presentation using .Net Core. The original code received events from Event Grid, but it is easily modified to use a simple webhook.

The controller defines an endpoint to receive the event and passes it along to SignalR:

[ApiController]
[Produces("application/json")]
public class EventHandlerController : Controller
{
    private readonly IHubContext<NotificationHub> _hubContext;

    public EventGridEventHandlerController(IHubContext<NotificationHub> hubContext)
    {
        _hubContext = hubContext;
    }

    [HttpPost]
    [Route("api/EventHandler")]
    public IActionResult Post([FromBody]object request)
    {

        object[] args = { request };

        _hubContext.Clients.All.SendCoreAsync("SendMessage", args);

        Console.WriteLine(truck);

        return Ok();
    }
}

There isn't any special code in the hub class-- you just define it as normal:

public class NotificationHub : Hub
{
    public async Task SendMessage(Message message)
    {
        await Clients.All.SendAsync("Notification", message);
    }
}

The client is also a standard SignalR implementation.

You can find a more generic SignalR tutorial here: https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.2&tabs=visual-studio

You can also use SignalR as a service or Notification Hub to achieve this result.

PerfectlyPanda
  • 3,271
  • 1
  • 6
  • 17