1. Introduction to MessagePipe
can be used for.NET
cap (a poem)Unity
High-performance in-memory/distributed messaging pipeline for the above. Suitable for publish/subscribe mode, CQRS intermediary mode, EventAggregator in Prism, IPC (inter-process communication)-RPC, etc.
Support:
- dependency injection
- Filter Piping
- Better Events
- Synchronous/asynchronous
- Keyed/unkeyed
- Singleton/Scoped
- Broadcast/response (+more)
- In-memory/inter-process/distributed
MessagePipe
Faster than standard C# event and 78 times faster than Prism's EventAggregator. Here's a screenshot of the official test
Each usepublish
Allocate less memory
A Roslyn analyzer is also provided to prevent subscription leaks.
This library usesMIT
protocols
2. MessagePipe distributed publish/subscribe usage (inter-process communication)
2.1 Create Two New Wpf Projects
The project names are:MessagePipePublishApp
cap (a poem)MessagePipeSubscribeApp
The Publish project is used to publish messages and the Subscribe project is used to receive messages. The project is built using the Prism framework. By overloading theCreateContainerExtension
method for dependency injection.
The two projects have had a significant impact onMessagePipe
The function injection code is as follows:
PublishProject Code:
var services = new ServiceCollection();
services
.AddMessagePipe()
.AddUdpInterprocess(
"127.0.0.1",
3215,
options =>
{
= ;
}
);
SubscribeProject Code:
var services = new ServiceCollection();
services
.AddMessagePipe()
.AddUdpInterprocess(
"127.0.0.1",
3215,
options =>
{
= ;
}
);
2.2 Implementation code
Two projects are injected via constructor injectionIDistributedPublisher
Interface.
PublishProject, through a button command to achieve the function of sending messages: the implementation code is as follows:
[RelayCommand]
private async Task PublishAsync()
{
await _distributedPublisher.PublishAsync(
"abc",
$"Message:{:yyyy-MM-dd HH:mm:}"
);
}
button is clicked once to send a message.
SubscribeIn the project, the Loaded method of the window is implemented to receive subscription messages and add them to the list for display.
[RelayCommand]
private async Task LoadedAsync()
{
await _distributedSubscriber.SubscribeAsync(
"abc",
message =>
{
(() =>
{
(message);
});
}
);
}