Skip to content

Repository files navigation

KnightBus

Build status NuGet Docs

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

Find the official KnightBus documentation here

KnightBus Logo

Transports

Package NuGet
KnightBus.Azure.ServiceBus KnightBus.Azure.ServiceBus
KnightBus.Azure.ServiceBus.Messages KnightBus.Azure.ServiceBus.Messages
KnightBus.Azure.ServiceBus.Management KnightBus.Azure.ServiceBus.Management
KnightBus.Azure.Storage KnightBus.Azure.Storage
KnightBus.Azure.Storage.Management KnightBus.Azure.Storage.Management
KnightBus.Azure.Storage.Messages KnightBus.Azure.Storage.Messages
KnightBus.Redis KnightBus.Redis
KnightBus.Redis.Messages KnightBus.Redis.Messages
KnightBus.Nats KnightBus.Nats
KnightBus.Nats.Messages KnightBus.Nats.Messages
KnightBus.PostgreSql KnightBus.PostgreSql
KnightBus.PostgreSql.Messages KnightBus.PostgreSql.Messages
KnightBus.PostgreSql.Management KnightBus.PostgreSql.Management

Monitoring

Package NuGet
KnightBus.ApplicationInsights KnightBus.ApplicationInsights
KnightBus.NewRelic KnightBus.NewRelic
KnightBus.OpenTelemetry KnightBus.OpenTelemetry

Serialization

Package NuGet
KnightBus.Newtonsoft KnightBus.Newtonsoft

Framework

Package NuGet
KnightBus.Host KnightBus.Host
KnightBus.Core KnightBus.Core
KnightBus.Core.Management KnightBus.Core.Management
KnightBus.Messages KnightBus.Messages
KnightBus.SqlServer KnightBus.SqlServer
KnightBus.Schedule KnightBus.Schedule

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 async Task Main(string[] args)
    {
            var host = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args)
            .ConfigureServices(services =>
            {
                //Multiple active transports
                services.UseServiceBus(config => config.ConnectionString = "sb-connection")
                        .UseTransport<ServiceBusTransport>()
                        .UseBlobStorage(config => config.ConnectionString = "storage-connection")
                        .UseTransport<StorageTransport>()
                        .RegisterProcessors();
            })
            .UseKnightBus().Build();                

            await host.StartAsync(CancellationToken.None);
    }
}

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, IPipelineInformation pipelineInformation, IMessageProcessor next, CancellationToken cancellationToken) where T : class, IMessage
        {
            try
            {
                await _semaphoreQueue.WaitAsync().ConfigureAwait(false);
                await next.ProcessAsync(messageStateHandler, cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                _semaphoreQueue.Release();
            }
        }
    }

Write your own Plugin

KnightBus supports custom plugins. Examples of existing plugins are: TcpAliveListener (K8S liveness probes) and Scheduling (Chron triggers).

public class CustomPlugin : IPlugin
{
    public CustomPlugin(ISomeDependency dependency, ILogger<CustomPlugin> logger)
    {        
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        // Start the plugin
    }
}

Repository layout

src/        One folder per published package
tests/      One folder per test project, plus the shared integration base classes
samples/    Runnable example applications
docs/       The documentation site, and the brand assets

CONTRIBUTING.md covers building, testing, formatting and releasing.

Documentation

The documentation site is built with MkDocs Material from the docs folder and deployed to GitHub Pages when a push to master touches docs/ or mkdocs.yml.

To preview it locally with live reload:

$ python3 -m venv .venv && source .venv/bin/activate
$ pip install -r docs/requirements.txt
$ mkdocs serve

Then open http://127.0.0.1:8000. Before pushing, run the same build CI runs, which turns broken links and pages missing from the navigation into errors:

$ mkdocs build --strict

Examples

Several examples of KnightBus usage can be found in the samples folder.

Azure Service Bus Aspire Host OpenTelemetry Example

An example of an Aspire Host using KnightBus with Azure Service Bus can be found in the samples/KnightBus.Samples.Azure.AspireHost folder.

You can run the example Service Bus Aspire Host locally by executing the following command in the samples/KnightBus.Samples.Azure.AspireHost folder:

dotnet run 

About

Fast multi-transport messaging framework

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

46 stars

Watchers

11 watching

Forks

Releases

Packages

Used by

Contributors

Languages