Receiving Webhooks
The Deveel.Webhooks.Receiver
library provides a set of components that can be used to receive webhooks from external sources, and handle them in an ASP.NET Core application.
The following sections describe how to use the library to receive custom webhooks in an ASP.NET Core application, and how to handle them, but other out-of-the-box implementations are also provided for specific providers (eg. Facebook, Twilio, SendGrid, etc.): please check the specific section for the configuration of your application to receive webhooks from those providers.
Installation
To start receiving webhooks from external sources, you can use the Deveel.Webhooks.Receiver.AspNetCore
library, that allows the registration of a webhook receiver in an ASP.NET Core application.
Run this command on the root of your project to install the library from NuGet:
Instrumenting the Application
If you are using a traditional ASP.NET Core MVC application, you can register the webhook receiver service by modifying the Startup
class as follows:
Alternatively, if you are using the mininal API pattern, you can use the following code:
The above simple calls registers the webhook receiver in the application, and enable it to receive webhooks of type MyWebhook
: receivers are segregated by the type of webhook they can handle, and you can register multiple receivers for different types of webhooks.
By default the registration of the webhook receiver adds a set of default services, that are required to handle the webhooks, such as the IWebhookReceiver<MyWebhook>
and IWebhookHandler<MyWebhook>
, and a default set of options: you can control further the services and configurations by using the builder instance returned by the AddWebhookReceiver
method.
Receiving Webhooks - Using Controllers
Following the registration of the webhook receiver, you can receive webhooks by using the IWebhookReceiver<MyWebhook>
service, that is registered in the application, if you want to handle the receive process directly.
This approach is typical in MVC APIs that implement the request processing in the controller, and can be used as follows:
Mind that in the above scenario you must also inject the IWebhookHandler<MyWebhook>
service, that is used to handle the received webhook.
Note - The design of the receiver allows the registration of multiple handlers for the same type of webhook, which can be injected in the controller as an IEnumerable<IWebhookHandler<MyWebhook>>
service, and can be used to handle the webhook in different ways. For simplicity of the example, we are using a single handler.
Receiving Webhooks - Using Middlewares
Alternatively the Deveel.Webhooks.Receiver.AspNetCore
library provides a middleware that can be used to receive webhooks, and handle them automatically.
To use the middleware, you must first register it in the Startup
class of your application:
If you are using the minimal API pattern, you can use the following code:
The above code registers the middleware in the application, and enables the application to receive webhooks of type MyWebhook
at the /webhook
endpoint, using the configurations defined when registering the receiver,
The middlware will automatically scan for all the registered webhook receiver service configured, and will handle the received webhooks by invoking all the IWebhookHandler<MyWebhook>
services registere.
The middleware design allows to handle the webhooks without any prior registered handler, by specifying an handling delegate in the MapWebhook
method:
Or a alternatively a synchronous handling delegate:
Further Reading
If you want to learn more and learn more advanced usage of the receivers, visit the Advanced Usage of Receiver.
Last updated