KnightBus is a fast, lightweight and extensible messaging framework that supports multiple active message transports
Find the official KnightBus documentation here
public class CommandProcessor : IProcessCommand<SampleCommand, SampleSettings>,
{
public CommandProcessor(ISomeDependency dependency)
{
//You can use your own container for dependency injection
}
public Task ProcessAsync(SampleCommand message, CancellationToken cancellationToken)
{
//Your code goes here
return Task.CompletedTask;
}
}
class Program
{
static void Main(string[] args)
{
MainAsync().GetAwaiter().GetResult();
}
static async Task MainAsync()
{
var knightBusHost = new KnightBusHost()
//Multiple active transports
.UseTransport(new ServiceBusTransport("sb-connection"))
.UseTransport(new StorageBusTransport("storage-connection"))
.Configure(configuration => configuration
.UseMessageProcessorProvider(new StandardMessageProcessorProvider()
.RegisterProcessor(new CommandProcessor()))
);
await knightBusHost.StartAndBlockAsync();
}
}
KnightBus supports inserting your own middleware into the execution pipeline.
public class CustomThrottlingMiddleware : IMessageProcessorMiddleware
{
private readonly SemaphoreQueue _semaphoreQueue;
public int CurrentCount => _semaphoreQueue.CurrentCount;
public CustomThrottlingMiddleware(int maxConcurrent)
{
_semaphoreQueue = new SemaphoreQueue(maxConcurrent);
}
public async Task ProcessAsync<T>(IMessageStateHandler<T> messageStateHandler, IMessageProcessor next, CancellationToken cancellationToken) where T : class, IMessage
{
try
{
await _semaphoreQueue.WaitAsync().ConfigureAwait(false);
await next.ProcessAsync(messageStateHandler, cancellationToken).ConfigureAwait(false);
}
finally
{
_semaphoreQueue.Release();
}
}
}