Skip to content

viktor-h/knightbus

 
 

Repository files navigation

KnightBus

Build status NuGet Documentation Status

KnightBus is a fast, lightweight and extensible messaging framework that supports multiple active message transports

Find the official KnightBus documentation here

KnightBus Logo

Package NuGet Stable
KnightBus.Host KnightBus.Host
KnightBus.Core KnightBus.Core
KnightBus.Messages KnightBus.Messages
KnightBus.Azure.ServiceBus KnightBus.Azure.ServiceBus
KnightBus.Azure.ServiceBus.Messages KnightBus.Azure.ServiceBus.Messages
KnightBus.Azure.Storage KnightBus.Azure.Storage
KnightBus.Azure.Storage.Messages KnightBus.Azure.Storage.Messages
KnightBus.Serilog KnightBus.Serilog
KnightBus.SimpleInjector KnightBus.SimpleInjector

Message Processing

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;
    }
}

Initialization

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();
    }
}

Bring your own Middleware

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();
            }
        }
    }

About

Fast multi-transport messaging framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%