AI agents: use the documentation index at llms.txt to locate machine-readable pages. This section is indexed by https://docs.particular.net/samples/llms.txt. The markdown version of this page is served as plain text. An MCP server at /mcp serves the same content via the search_docs and read_doc tools; it is read-only and needs no credentials. Markdown versions of documentation pages are available by appending .md to the page URL. Directory URLs use index.md. They are served as text/plain because some retrieval backends reject text/markdown.

ASP.NET Core Dependency Injection Integration

NuGet Package:
NServiceBus 9.x

ASP.NET Core has an integrated dependency injection (DI) feature. When hosting NServiceBus endpoints inside an ASP.NET Core app, sharing components registered for DI between ASP.NET components and NServiceBus message handlers may be necessary. Use the NServiceBus.Extensions.Hosting package to host an endpoint as part of an ASP.NET Core application.

Configuring an endpoint to use built-in DI

builder.Services.AddSingleton<MyService>();

var endpointConfiguration = new EndpointConfiguration("Sample.Core");
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.UseTransport<LearningTransport>();

builder.UseNServiceBus(endpointConfiguration);

When the sample is run, a web application is started. All web requests received will trigger a message send operation:

app.MapGet("/", context =>
{
    var endpointInstance = context.RequestServices.GetService<IMessageSession>();
    var myMessage = new MyMessage();

    return Task.WhenAll(
        endpointInstance.SendLocal(myMessage),
        context.Response.WriteAsync("Message sent"));
});

Message handlers will have dependencies injected at runtime by the configured Inversion of Control (IoC) container:

public class MyHandler : IHandleMessages<MyMessage>
{
    MyService myService;

    public MyHandler(MyService myService)
    {
        this.myService = myService;
    }

    public Task Handle(MyMessage message, IMessageHandlerContext context)
    {
        myService.WriteHello();
        return Task.CompletedTask;
    }
}

Configuring to use shared DI with Autofac

It is also possible to configure ASP.NET Core to use a specific container and still share components between ASP.NET and NServiceBus message handlers. This sample demonstrates how to do this with Autofac using the Autofac.Extensions.DependencyInjection package

First, ASP.NET Core is instructed to use a custom container:

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());

Then, Startup can use Autofac natively to configure services:

builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder => containerBuilder.RegisterType<MyService>().SingleInstance());

The ConfigureServices method is called by .NET Core at application startup time to register additional services. The ConfigureContainer method registers the components in the container using the container's native APIs.

Related Articles