InMemory transport for Muflone, providing an in-process service bus to dispatch commands and publish events without any external broker dependency. Ideal for development, testing, and lightweight production scenarios.
- .NET 10.0+
Muflone10.0.1+
Package Manager
Install-Package Muflone.Transport.InMemory.NET CLI
dotnet add package Muflone.Transport.InMemoryCall AddMufloneTransportInMemory on your IServiceCollection during application startup:
builder.Services.AddMufloneTransportInMemory();This registers the following services:
| Service | Implementation | Lifetime |
|---|---|---|
IMessageSubscriber |
InMemorySubscriber |
Singleton |
IServiceBus |
InMemoryBus |
Singleton |
IEventBus |
InMemoryBus |
Singleton |
MessageHandlersStarter |
Hosted service | — |
Inject IServiceBus and call SendAsync:
public class MyService(IServiceBus serviceBus)
{
public async Task DoSomethingAsync(CancellationToken cancellationToken)
{
var command = new MyCommand(Guid.NewGuid());
await serviceBus.SendAsync(command, cancellationToken);
}
}Inject IEventBus and call PublishAsync:
public class MyService(IEventBus eventBus)
{
public async Task NotifyAsync(CancellationToken cancellationToken)
{
var @event = new MyEvent(Guid.NewGuid());
await eventBus.PublishAsync(@event, cancellationToken);
}
}InMemoryBus implements both IServiceBus (commands) and IEventBus (events). When a message is dispatched, it looks up registered subscribers via InMemorySubscriber and writes the message directly into each subscriber's .NET Channel<object>.
InMemorySubscriber maintains a ConcurrentDictionary mapping message types to their IInMemoryChannel instances. Each subscriber gets an unbounded System.Threading.Channels.Channel that is read asynchronously and forwarded to the corresponding handler. Namespace-agnostic lookup is also supported, so commands/events with the same type name but different namespaces are matched correctly.
MufloneBroker provides a lightweight static alternative that stores dispatched messages in ObservableCollection properties — useful in tests or simple scenarios:
MufloneBroker.Send(command); // adds to MufloneBroker.Commands
MufloneBroker.Publish(@event); // adds to MufloneBroker.EventsSee a working example at BrewUp.
- Alberto Acerbis
- Alessandro Colla
This project is licensed under the MIT License.