-
-
Notifications
You must be signed in to change notification settings - Fork 12
Eventing
- Summary
- Configuration
- Eventing Provider
- Eventing Registration
- Publishing Event
- Subscription Event Handler
- Health Checks
- Injected Services
Nano supports different ways of publishing and subscribing to events.
Adding eventing annotations to model implementations, provides a way of synchronizing entities between applications. Additionally, custom event models can be implemented and published, and consumed through subscriptions.
The IEventingProvider
is registered during startup, and the implementing type defines the eventing provider used in the application. Furthermore, the interface IEventing
is registered as well, and defines the entry to publishing and subscribing to events.
The Eventing
section of the configuration defines the behavior of eventing (message queuing) in the application.
The section is serialized into an instance of EventingOptions
, and injected as dependency during startup, thus available for injection throughout the application.
See Appendix - App Settings - Eventing for details about the section and the meaning of the variables.
"Eventing": {
"Host": null,
"VHost": null,
"Username": null,
"Password": null,
"Port": 5672,
"UseSsl": false,
"Timeout": 30,
"Heartbeat": 60,
"PrefetchCount": 50,
"UseHealthCheck": true,
"UnhealthyStatus": "Unhealthy"
}
Nano provides several eventing providers (and more added on request), so usually there is no need to implement a custom data provider for your application.
Eventing providers implements the interface IEventingProvider
. It contains a single method Configure(...)
, that is responsible for handling any configuration and setup required for the eventing provider.
The eventing providers currently supported by Nano, can be referenced in the Appendix - Supported Providers.
The eventing provider must be registered as dependencies.
Invoke the method .AddEventing<TProvider>();
, using the eventing provider implementation as generic type parameters.
.ConfigureServices(x =>
{
x.AddEventing<EasyNetQProvider>();
})
By default, IEventing
resolves to NullEventing
, which as the name indicates doesn't publish or subscribe to anything. Registering an eventing provider dependency, will override the NullEventing
with the configured eventing provider implementation.
An event can be published from anywhere within the application.
Nano controllers has the IEventing
dependency injected, and publishing from one of it's actions is recommended. Either override an existing inherited action or implement a new one.
public void Publish()
{
var @event = new MyEvent();
this.Eventing.PublishAsync(@event);
}
Subscribing to an event requires an event handler implementation.
Implementing the interface IEventingHandler<TEvent>
ensures that the event handler is registered during startup, and the subscription is configured to listen for incoming events. The method CallbackAsync(...)
is invoked for evert event received.
It's recommended to share the event model, as both the publisher and subscriber must understand the object model.
public class MyEventHandler : IEventingHandler<MyEvent>
{
protected virtual IService Service { get; }
public MyEventHandler(IService service)
{
this.Service = service;
}
public void CallbackAsync(MyEvent @event, bool isRetrying)
{
// Event logic.
}
}
When enabling health-checks in the eventing section of the confiugration, the application will be configured with a health-check for the eventing provider. When the application starts, a check is made to ensure that the eventing provider is up and running, returning a healthy status code when checked.
The health status of the application, including the eventing provider, can be found here:
http://{host}:{port}/healthz
When building the eventing provider, dependencies related to eventing is configured and initialized.
Nano.Eventing.EventingOptions
Nano.Eventing.Interfaces.IEventing
Nano.Eventing.Interfaces.IEventingProvider
Nano.Eventing.Interfaces.IEventingHandler<>
For a full list of services and dependencies, see Injected Dependencies