joydip_kanjilal
Contributor

Introducing ASP.Net WebHooks

opinion
Mar 31, 20164 mins
Software DevelopmentTechnology IndustryWeb Development

Take advantage of ASP.Net Web Hooks to send HTTP POST notifications within the context of the ASP.Net runtime

Essentially, WebHooks is a pattern — its implementation varies from service to service. However, the basic concepts are all the same: You leverage a publisher/subscriber model. When an event occurs, a notification is sent via HTTP POST over HTTP. ASP.Net WebHooks represent a lightweight pattern that provides support for a publisher/subscriber model over HTTP. The publisher/subscriber pattern is a messaging pattern in which the publishers are not programmed to send messages to specific subscribers, and the subscribers receive only those messages that they need or are relevant. This model providing increased scalability.

ASP.Net WebHooks is an exciting new feature that has been added to the ASP.Net stack. ASP.Net WebHooks is open source and, as of this writing, is being actively developed. There are many online services that take advantage of WebHooks to notify actions. These include: GitHub, Trello, Dropbox, Salesforce, WordPress, and more. WebHooks are nothing new, but they are poised to be the technology of choice for building RESTful services that need to execute tasks and send notifications.

MSDN states

WebHooks is a pattern which means that it varies how it is used from service to service but the basic idea is the same. You can think of WebHooks as a simple pub/sub model where a user can subscribe to events happening elsewhere. The event notifications are propagated as HTTP POST requests containing information about the event itself.  

How does it all work?

When a WebHook is raised, here’s the sequence of steps that happen in a typical WebHook flow.

  1. The WebHook publisher first exposes events that can be subscribed to by the WebHook subscribers.
  2. The WebHook subscriber now subscribes to the WebHook.
  3. As and when an event occurs, HTTP POST callbacks are sent to the registered subscribers.
  4. The WebHook receiver then validates the request and, if all’s fine, it passes it to a WebHook handler.
  5. The WebHook handler then provides your application access to the data that has been received through the WebHook.

Receivers and handlers

Note that the two major concepts you should understand to work with WebHooks include receivers and handlers. While the former is responsible for accepting WebHooks and verifying their integrity, the latter is used to process a particular WebHook.

WebHooks and SignalR

ASP.Net WebHooks are used to send and receive hooks within the context of the ASP.Net engine. What’s the difference then between WebHooks and SignalR — are they both the same? Not really. WebHooks and SingalR can be complementary to each other. WebHooks is a technology that enables you to receive notifications via HTTP POST requests. Such notifications are pushed in real time without needing to hold the network connections open. In essence, there isn’t any need to hold connections open while waiting for events.

SignalR is an open source library written in .Net and doesn’t leverage the request-response model to establish communications between the server and the client. Rather, it uses Remote Procedure Call (RPC) to enable the server-side application to notify the Web browser at the client side with updates and notifications. SignalR is an abstraction over a TCP connection. In essence, you can leverage SignalR for notifications within a .Net application through WebSockets. If your application receives a notification from Dropbox, you can notify a user of the application using SignalR or Web Sockets.

Where do I get ASP.Net WebHooks?

You can install ASP.Net WebHooks via NuGet Package Manager in Visual Studio. Alternatively, you can install the packages from Package Manager Console Window. All of these packages are grouped into Common, Sender, and Receiver categories. If you want to work with ASP.Net WebHooks, you must install the right WebHook provider. For each service from which you would like to receive WebHooks, you should install the corresponding provider.

The NuGet packages in the Receiver category are named Microsoft.AspNet.WebHooks.Receivers.*. The * in the name implies the service you would like to connect to. For example, if you were to receive WebHooks from GitHub, you would need to install Microsoft.AspNet.WebHooks.Receivers.GitHub. Note: You should install Microsoft.AspNet.WebHooks.Receivers.Custom if you would like to receive WebHooks sent by ASP.Net WebHooks.

You can find out more about ASP.Net WebHook from Microsoft’s documentation.

I will post a series of articles on how we can program ASP.Net WebHooks soon.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author