🚌 Microsoft Extensions Dependency Injection container adapter for Rebus
Branch: master
Clone or download
Latest commit 3e19ebc Feb 1, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github now here Aug 31, 2016
Rebus.ServiceProvider.Tests IContainerAdapter isn't needed Feb 1, 2019
Rebus.ServiceProvider IContainerAdapter isn't needed Feb 1, 2019
Sample.ConsoleApp
Sample.WebApp minor cleanup, update changelog Feb 1, 2019
scripts update scripts Apr 30, 2017
tools update Aversion Oct 26, 2017
.gitignore fix ignore file Sep 9, 2016
CHANGELOG.md minor cleanup, update changelog Feb 1, 2019
CONTRIBUTING.md now here Aug 31, 2016
LICENSE.md
README.md Update README.md Jan 31, 2019
Rebus.ServiceProvider.sln First pass on v5 rework Jan 31, 2019
appveyor.yml .net core support Apr 30, 2017

README.md

Rebus.ServiceProvider

install from nuget

Provides an Microsoft.Extensions.DependencyInjection-based container adapter for Rebus.


Usage

In ASP.NET Core 2.0+ Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Register handlers 
    services.AutoRegisterHandlersFromAssemblyOf<Handler1>();

    // Configure and register Rebus
    services.AddRebus(configure => configure
        .Logging(l => l.Use(new MSLoggerFactoryAdapter(_loggerFactory)))
        .Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "Messages"))
        .Routing(r => r.TypeBased().MapAssemblyOf<Message1>("Messages")));
}
       
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.ApplicationServices.UseRebus();
    //or optionally act on the bus
    //app.ApplicationServices.UseRebus(async bus => await bus.Subscribe<Message1>());

    app.Run(async (context) =>
    {
        var bus = app.ApplicationServices.GetRequiredService<IBus>();
        var logger = _loggerFactory.CreateLogger<Startup>();

        logger.LogInformation("Publishing {MessageCount} messages", 10);

        await Task.WhenAll(
            Enumerable.Range(0, 10)
                .Select(i => new Message1())
                .Select(message => bus.Send(message)));

        await context.Response.WriteAsync("Rebus sent another 10 messages!");
    });
}

(See the WebApp sample)

A vanilla console app

var services = new ServiceCollection();

// Automatically register all handlers from the assembly of a given type...
services.AutoRegisterHandlersFromAssemblyOf<Handler1>();

//Configure Rebus
services.AddRebus(configure => configure
    .Logging(l => l.ColoredConsole())
    .Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "Messages"))
    .Routing(r => r.TypeBased().MapAssemblyOf<Message1>("Messages")));

// Potentially add more service registrations for the application, some of which
// could be required by handlers.

// Make sure we correctly dispose of the provider (and therefore the bus) on application shutdown
using (var provider = services.BuildServiceProvider())
{
    // Application starting...

    // Now application is running, lets trigger the 'start' of Rebus.
    provider.UseRebus();
    
    //optionally...
    //provider.UseRebus(async bus => await bus.Subscribe<Message1>());
}

(See the ConsoleApp sample)