Learn about notification handlers lifetime, async notification handler and how to register the notification handlers.
It's important to know that the handlers you create and register to receive notifications will be transient. This means that they will be initialized every time they receive a notification. You can therefore not rely on them having a specific state based on previous notifications.
As an example, you cannot do the following:
Create a list in a handler.
Add something when a notification is received.
Check if that list contains what you added in an earlier notification.
When following the steps above, the list will always be empty because the object has only been initialized.
If you need persistence between notifications, we recommend you move that functionality into a service or similar. You can then register it with the DI container, and inject it into your handler.
As previously mentioned, many notifications exist in pairs, with a "before" and "after" notification. There may be cases where you want to add some information to the "before" notification, which will then be available to your "after" notification handler. In order to support this, the notification "pairs" are stateful. This means the notifications contain a dictionary that is shared between the "before" and "after" notifications. You can add values to the dictionary, and later retrieve them like this:
Once you have made your notification handlers, you need to register them with the AddNotificationHandler
extension method on the IUmbracoBuilder
. This enables them to run whenever a notification they subscribe to is published. There are two ways to do this:
In the Program class, if you're making handlers for your site
In a composer, if you're a package developer subscribing to notifications
Learn more about the different ways of registering your handlers and other extensions in the Dependency Injection article.
In the Program.cs
file, register your notification in the CreateUmbracoBuilder()
builder chain:
The extension method takes two generic type parameters. The first, ContentPublishingNotification
, is the notification you wish to subscribe to. The second, DontShout
, is the class that handles the notification. This class must implement INotificationHandler<>
with the type of notification it handles as the generic type parameter. In this case, the DontShout
class definition looks like this:
For the full handler implementation, see ContentService Notifications.
If you are writing a package for Umbraco you will not have access to the Program class. You can instead use a composer, which gives you access to the IUmbracoBuilder
.
If you need to do anything asynchronously when handling a notification, you can achieve this using the INotificationAsyncHandler
.
Create an asynchronous handler by implementing the INotificationAsyncHandler
:
When using the INotificationAsyncHandler
, register it using the IUmbracoBuilder
and the AddNotificationAsyncHandler
extension method. This can be done in the Program class or with a composer.