.NET Events

The event model used in the .NET API is of the type pub/sub and events are only fired in the same application that raised the event.

If you have two servers, A and B running Litium, for example, and a back-office user is working against A (load balancer decides which server they are connected to), and creates a currency, the CurrencyCreatedEvent listener running in A will get triggered, but the same listener that is running in B will not get triggered. 

Event broker service

The event broker is used to handle events. When using multiple applications, web farms or multiple developers with the same database, the event synchronization between all applications are made with a service bus. The event broker supports both synchroinious and asynchronious publishing and subscription.

In multi server environments, Service bus need to be setup for events to work between servers.

The service Litium.Events.EventBroker is responsible to:

  • subscribe event notifications
  • publish events to all subscriptions.

When registered for an event, the event broker will return a handle for the actual listener, the handle will be used to unsubscribe the event.

using Litium.Runtime;

[Autostart]
public class EventBrokerExample : IDisposable
{
    private readonly Litium.Events.ISubscription<Litium.Globalization.Events.CurrencyCreated> _subscription;

    public EventBrokerExample(Litium.Events.EventBroker eventBroker)
    {
        _subscription = eventBroker.Subscribe<Litium.Globalization.Events.CurrencyCreated>(currencyEvent =>
        {
            // event handler for Litium.Globalization.Events.CurrencyCreated event.
        });
    }

    public void Dispose()
    {
        // unregister the event from event broker
        _subscription.Dispose();
    }
}

To publish an event the EventBroker.Publish method is used.

public class EventBrokerNotifyExample
{
    private readonly EventBroker _eventBroker;

    public EventBrokerNotifyExample(EventBroker eventBroker)
    {
        _eventBroker = eventBroker;
    }

    public void Send()
    {
        // send event with the event broker
        _eventBroker.Publish(new MyEvent(new Currency("SEK")));
    }
}
Was this page helpful?
Thank you for your feedback!