CodeWF.EventBus.Socket
CodeWF.EventBus.Socket 是一个面向 C# 进程间通信的轻量级 TCP 事件总线库。它基于 Socket 提供发布订阅与 Query 式请求响应能力,让本地多进程或轻量服务之间可以通信,而不必先引入 RabbitMQ、Kafka、Redis 等外部 MQ 基础设施。
characteristics
- 基于
CodeWF.NetWrapper的 TCP Socket 传输。 - Support Publish/Subscribe cross-process event notification.
- Support Query/Response interactions under the same theme.
- 查询请求按
TaskId关联,支持同主题并发查询。 - It does not rely on third-party MQ and is suitable for lightweight event distribution and IPC scenarios.
- 自带示例工程
src/EventBusDemo。
installation
Install-Package CodeWF.EventBus.Socket
You can also use the. NET CLI:
dotnet add package CodeWF.EventBus.Socket
quick start
Start the server:
using CodeWF.EventBus.Socket;
IEventServer eventServer = new EventServer();
await eventServer.StartAsync("127.0.0.1", 9100);
Connecting the client:
using CodeWF.EventBus.Socket;
IEventClient eventClient = new EventClient();
await eventClient.ConnectAsync("127.0.0.1", 9100);
Subscribe and publish events:
eventClient.Subscribe<NewEmailCommand>("event.email.new", command =>
{
Console.WriteLine($"收到邮件主题:{command.Subject}");
});
eventClient.Publish("event.email.new", new NewEmailCommand
{
Subject = "欢迎使用",
Content = "您的账号已经准备完成",
SendTime = DateTime.Now
}, out _);
Query/Response:
eventClient.Subscribe<EmailQuery>("event.email.query", query =>
{
var response = new EmailQueryResponse
{
Emails = EmailManager.QueryEmail(query.Subject)
};
eventClient.Publish("event.email.query", response, out _);
});
var result = await eventClient.QueryAsync<EmailQuery, EmailQueryResponse>(
"event.email.query",
new EmailQuery { Subject = "账户" },
3000);
using boundary
This library is suitable for lightweight event distribution, native multi-process communication, and small inter-service notifications. Currently, messages are only stored in memory and do not provide persistence, retry queues or delivery guarantees after process restarts; if used in a production environment, authentication, encryption, monitoring, current limit and retry policies should be supplemented according to business needs.