MessageBus

MessageBus instance handles publishers and subscribers registering and unregistering, hosts sequences for each message name and executes message when publisher requests.

Instance

To use MessageBus, an instance of MessageBus need to be created for registering publishers and subscribers, hosting sequences for each message name and executing message when publisher requests.

C#
using var bus = new MessageBus();

MessageBus implements IDisposable. When disposing, all publishers and subscribers are unregistered. No unmanaged resource is used.

Shrink

By default, AutoShrink is on. When a publisher is unregistered, MessageBus checks to see if there are still publishers associated with the same message name. If not, related resources will be released. When the publishers are frequently unregistered and registered, the related checking and recycling work may affect the efficiency. In this case, turn off AutoShrink and call ShrinkSequencers manually when required.

Hello, World!

Uses MessageBus to execute subscriber with parameter when publisher requests.

In code below, one publisher and one subscriber is registered with the name Hello. The method of subscriber is called when publisher requests.

Uses MessageBus to Relay Messages.

Subscribers have a parameter to receive data from Publisher as argument.

Here is the full code.

C#
public void TestMethod()
{
    using var bus = new MessageBus();
    var subscriberTicket = bus.RegisterSubscriber<string>("Hello", SubscriberMethod);
    var text = "Hello World";

    var publisherTicket = bus.RegisterPublisher<string>("Hello");
    publisherTicket.Executor.Execute(text);

    bus.UnregisterPublisher(publisherTicket);
    bus.UnregisterSubscriber(subscriberTicket);
}

private string? _received;
public void SubscriberMethod(string? argument)
{
    _received = argument;
}

To register a subscriber, uses the code below. Check Subscriber for details.

C#
var subscriberTicket = bus.RegisterSubscriber<string>("Hello", SubscriberMethod);

To register a publisher, uses the code below. Check Publisher for details.

C#
var publisherTicket = bus.RegisterPublisher<string>("Hello");
publisherTicket.Executor.Execute(text);

When Execute calling, SubscriberMethod is executed.

Uses MessageBus to Relay Messages and Get Return Data.

Unlike many publisher subscribe model, MessageBus supports return data from subscriber to publisher.

Here is the full code.

C#
public void TestMethod()
{
    using var bus = new MessageBus();
    var subscriberTicket = bus.RegisterSubscriber<string, int>("Hello", SubscriberMethod);
    var text = "Hello World";

    var publisherTicket = bus.RegisterPublisher<string, int>("Hello"); 
    var returnValue = publisherTicket.Executor.Execute(text);
    //after being executed: returnValue == 100

    bus.UnregisterPublisher(publisherTicket);
    bus.UnregisterSubscriber(subscriberTicket);
}

private string? _received;
public int SubscriberMethod(string? argument)
{
    _received = argument;
    return 100;
}

To register a subscriber, uses the code below. Check Subscriber for details.

C#
var subscriberTicket = bus.RegisterSubscriber<string, int>("Hello", SubscriberMethod);

To register a publisher, uses the code below. Check Publisher for details.

C#
var publisherTicket = bus.RegisterPublisher<string, int>("Hello"); 
var returnValue = publisherTicket.Executor.Execute(text);

When Execute calling, SubscriberMethod is executed and the return value is sent back to the caller.

See Also