An open-source lightweight message bus library (RabbitMQ) that supports publish/subscribe and RPC.
nuget | stats |
---|---|
You can install via NuGet:
Install-Package MyStack.DistributedMessage4RabbitMQ
# Getting Started
## Add Service Support
```csharp
services.AddDistributedMessage4RabbitMQ(configure =>
{
configure.HostName = "localhost";
configure.VirtualHost = "/";
configure.Port = 5672;
configure.UserName = "admin";
configure.Password = "admin";
configure.QueueOptions.Name = "MyStack";
configure.ExchangeOptions.Name = "MyStack";
configure.ExchangeOptions.ExchangeType = "topic";
configure.RPCTimeout = 2000;
},
Assembly.GetExecutingAssembly());
public class HelloMessage : IDistributedEvent
{
public string Message { get; set; }
}
or
[MessageName("HelloMessage")]
public class HelloMessage : IDistributedEvent
{
public string Message { get; set; }
}
public class HelloMessageHandler : IDistributedEventHandler<HelloMessage>
{
public async Task HandleAsync(HelloMessage message, CancellationToken cancellationToken)
{
Console.WriteLine("Hello");
await Task.CompletedTask;
}
}
await messageBus.PublishAsync(new HelloMessage() { Message = "Hello" });
public class WrappedData
{
public string Message { get; set; }
}
public class DistributedEventWrapperHandler : IDistributedEventHandler<DistributedEventWrapper<WrappedData>>
{
public async Task HandleAsync(DistributedEventWrapper<WrappedData> eventData, CancellationToken cancellationToken = default)
{
await Task.CompletedTask;
Console.WriteLine("DistributedEventWrapper");
}
}
await messageBus.PublishAsync(new WrappedData());
public class SubscribeData
{
}
[Subscribe("ABC")]
public class SubscribeDataHandler : IDistributedEventHandler
{
public async Task HandleAsync(object eventData, CancellationToken cancellationToken = default)
{
Console.WriteLine("SubscribeData");
await Task.CompletedTask;
}
}
await messageBus.PublishAsync("ABC",new SubscribeData());
public class Ping : IRpcRequest<Pong>
{
public string SendBy { get; set; }
}
public class Pong
{
public string ReplyBy { get; set; }
}
public class PingHandler : IRpcRequestHandler<Ping, Pong>
{
public Task<Pong> HandleAsync(Ping message, CancellationToken cancellationToken = default)
{
Console.WriteLine("Ping");
return Task.FromResult(new Pong() { ReplyBy = "B" });
}
}
var pongMessage = messageBus.SendAsync(ping);
MIT