Location>code7788 >text

Using MessagePipe for inter-process communication

Popularity:479 ℃/2024-09-28 14:04:39

1. Introduction to MessagePipe

can be used for.NETcap (a poem)UnityHigh-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 usepublishAllocate less memory

A Roslyn analyzer is also provided to prevent subscription leaks.

This library usesMITprotocols

2. MessagePipe distributed publish/subscribe usage (inter-process communication)

2.1 Create Two New Wpf Projects

The project names are:MessagePipePublishAppcap (a poem)MessagePipeSubscribeAppThe 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 theCreateContainerExtensionmethod for dependency injection.
The two projects have had a significant impact onMessagePipeThe 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 injectionIDistributedPublisherInterface.

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);
            });
        }
    );
}
2.3 Operational effects