Note
In the code above, the publisher is registered with IsAlwaysExecuteAll is on. This forces MessageBus to run through all matched subscribers. Check this topic for details.
Message name is the only condition to match subscribers to publishers.
MessageBus uses only name to match subscribers to publishers.
When publisher registering, each existing subscriber is checked to see whether the name matches the publisher. When subscriber registering, it is checked against each existing publisher for name matching and saved in pool for further publisher registering.
MessageBus supports multiple ways to check the message name.
Each publisher has one name specified exactly when registering. Each subscriber is supported to use multiple way to match one or more publishers by name. This demo presents multiple ways for name matching.
public void TestMethod()
{
using var bus = new MessageBus();
var subscriberExact1Ticket = bus.RegisterSubscriber<string>("Hello", SubscriberExact1);
var subscriberExact2Ticket = bus.RegisterSubscriber<string>("hello", SubscriberExact2);
var subscriberIgnoreCaseTicket = bus.RegisterSubscriber<string>(new MessageNameMatchingWithStringComparison("HELLO", StringComparison.OrdinalIgnoreCase), SubscriberIgnoreCase);
var subscriberAllTicket = bus.RegisterSubscriber<string>(new MessageNameMatchingAll(), SubscriberAll);
var subscriberRegEx1Ticket = bus.RegisterSubscriber<string>(new MessageNameMatchingWithRegularExpression("[Hh]ello"), SubscriberRegEx1);
var subscriberRegEx2Ticket = bus.RegisterSubscriber<string>(new MessageNameMatchingWithRegularExpression("hello"), SubscriberRegEx2);
var text = "Hello World";
var publisherTicket = bus.RegisterPublisher<string>("Hello", new MessageBusPublisherOptions<string>(true));
publisherTicket.Executor.Execute(text);
bus.UnregisterPublisher(publisherTicket);
bus.UnregisterSubscriber(subscriberExact1Ticket);
bus.UnregisterSubscriber(subscriberExact2Ticket);
bus.UnregisterSubscriber(subscriberIgnoreCaseTicket);
bus.UnregisterSubscriber(subscriberAllTicket);
bus.UnregisterSubscriber(subscriberRegEx1Ticket);
bus.UnregisterSubscriber(subscriberRegEx2Ticket);
//after being executed: _exact1, _ignoreCase, _all and _regex1 are equal to text; _exact2 and _regex2 are not.
}
private string? _exact1, _exact2, _ignoreCase, _all, _regex1 , _regex2;
public void SubscriberExact1(string? argument)
{
_exact1 = argument;
}
public void SubscriberExact2(string? argument)
{
_exact2 = argument;
}
public void SubscriberIgnoreCase(string? argument)
{
_ignoreCase = argument;
}
public void SubscriberAll(string? argument)
{
_all = argument;
}
public void SubscriberRegEx1(string? argument)
{
_regex1 = argument;
}
public void SubscriberRegEx2(string? argument)
{
_regex2 = argument;
}
Sub TestMethod()
Using bus As New MessageBus
Dim subscriberExact1Ticket = bus.RegisterSubscriber(Of String)("Hello", AddressOf subscriberExact1)
Dim subscriberExact2Ticket = bus.RegisterSubscriber(Of String)("hello", AddressOf SubscriberExact2)
Dim subscriberIgnoreCaseTicket = bus.RegisterSubscriber(Of String)(New MessageNameMatchingWithStringComparison("HELLO", StringComparison.OrdinalIgnoreCase), AddressOf SubscriberIgnoreCase)
Dim subscriberAllTicket = bus.RegisterSubscriber(Of String)(New MessageNameMatchingAll, AddressOf SubscriberAll)
Dim subscriberRegEx1Ticket = bus.RegisterSubscriber(Of String)(New MessageNameMatchingWithRegularExpression("[Hh]ello"), AddressOf SubscriberRegEx1)
Dim subscriberRegEx2Ticket = bus.RegisterSubscriber(Of String)(New MessageNameMatchingWithRegularExpression("hello"), AddressOf SubscriberRegEx2)
Dim text = "Hello World"
Dim publisherTicket = bus.RegisterPublisher(Of String)("Hello", New MessageBusPublisherOptions(Of String)(True))
publisherTicket.Executor.Execute(text)
bus.UnregisterPublisher(publisherTicket)
bus.UnregisterSubscriber(subscriberExact1Ticket)
bus.UnregisterSubscriber(subscriberExact2Ticket)
bus.UnregisterSubscriber(subscriberIgnoreCaseTicket)
bus.UnregisterSubscriber(subscriberAllTicket)
bus.UnregisterSubscriber(subscriberRegEx1Ticket)
bus.UnregisterSubscriber(subscriberRegEx2Ticket)
'after being executed: _exact1, _ignoreCase, _all and _regex1 are equal to text; _exact2 and _regex2 are not.
End Using
End Sub
Private _exact1, _exact2, _ignoreCase, _all, _regex1, _regex2 As String
Public Sub SubscriberExact1(argument As String)
_exact1 = argument
End Sub
Public Sub SubscriberExact2(argument As String)
_exact2 = argument
End Sub
Public Sub SubscriberIgnoreCase(argument As String)
_ignoreCase = argument
End Sub
Public Sub SubscriberAll(argument As String)
_all = argument
End Sub
Public Sub SubscriberRegEx1(argument As String)
_regex1 = argument
End Sub
Public Sub SubscriberRegEx2(argument As String)
_regex2 = argument
End Sub
Matches base on the message name comparison exactly. This is the default behaviour.
When subscriber is registered with a message name specified, the string is used to compare with each publisher exactly.
var subscriberExact1Ticket = bus.RegisterSubscriber<string>("Hello", SubscriberExact1);
var subscriberExact2Ticket = bus.RegisterSubscriber<string>("hello", SubscriberExact2);
Dim subscriberExact1Ticket = bus.RegisterSubscriber(Of String)("Hello", AddressOf subscriberExact1)
Dim subscriberExact2Ticket = bus.RegisterSubscriber(Of String)("hello", AddressOf SubscriberExact2)
The comparison is case sensitive. The first subscriber matches Hello
, which is equal to the name provided by publisher, allowing to be executed when publisher requests. The second one matching hello
, which is not equal to Hello
, will not be executed by the publisher in this demo.
When subscriber is registered with an instance of MessageNameMatchingWithStringComparison, StringComparison can be specified to be used in comparison. The default value is OrdinalIgnoreCase.
In this case, HELLO
is matched with Hello
, which is indicated with publisher registering, based on OrdinalIgnoreCase. This subscriber will be executed.
var subscriberIgnoreCaseTicket = bus.RegisterSubscriber<string>(new MessageNameMatchingWithStringComparison("HELLO", StringComparison.OrdinalIgnoreCase), SubscriberIgnoreCase);
Dim subscriberIgnoreCaseTicket = bus.RegisterSubscriber(Of String)(New MessageNameMatchingWithStringComparison("HELLO", StringComparison.OrdinalIgnoreCase), AddressOf SubscriberIgnoreCase)
When an instance of MessageNameMatchingAll is provided, the subscriber hooks all publishers, including existing and to be registered later.
var subscriberAllTicket = bus.RegisterSubscriber<string>(new MessageNameMatchingAll(), SubscriberAll);
Dim subscriberAllTicket = bus.RegisterSubscriber(Of String)(New MessageNameMatchingAll, AddressOf SubscriberAll)
A regular expression wrapped by MessageNameMatchingWithRegularExpression declares to use expression specified in name matching.
var subscriberRegEx1Ticket = bus.RegisterSubscriber<string>(new MessageNameMatchingWithRegularExpression("[Hh]ello"), SubscriberRegEx1);
Dim subscriberRegEx1Ticket = bus.RegisterSubscriber(Of String)(New MessageNameMatchingWithRegularExpression("[Hh]ello"), AddressOf SubscriberRegEx1)
Expression [Hh]ello
matches the value provided with publisher Hello
while expression hello
does not.
When complex model is required, uses your own class derived from MessageNameMatcherBase. IsComplied is called for each message name to be checked.