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.

Externally Managed Mode

Component:
NServiceBus
NuGet Package:
NServiceBus 9.x

Configuring the endpoint

This sample configures an endpoint to use externally managed mode with Microsoft's dependency injection container and registers some dependencies.

// ServiceCollection is provided by Microsoft.Extensions.DependencyInjection
var serviceCollection = new ServiceCollection();

// most dependencies may now be registered
serviceCollection.AddSingleton<Greeter>();
serviceCollection.AddSingleton<MessageSender>();

// EndpointWithExternallyManagedContainer.Create accepts an IServiceCollection,
// which is inherited by ServiceCollection
var endpointWithExternallyManagedContainer = EndpointWithExternallyManagedContainer
    .Create(endpointConfiguration, serviceCollection);

// if IMessageSession is required as dependency, it may now be registered
serviceCollection.AddSingleton(p => endpointWithExternallyManagedContainer.MessageSession.Value);

Injecting dependencies into handlers

Registered dependencies may be injected into message handlers using constructor injection:

public class MyHandler(Greeter greeter) : IHandleMessages<MyMessage>
{
    public Task Handle(MyMessage message, IMessageHandlerContext context)
    {
        greeter.SayHello();
        return Task.CompletedTask;
    }
}

Injecting the message session into other types

When IMessageSession has been registered as shown above, it may be injected into other types using constructor injection:

public class MessageSender(IMessageSession messageSession)
{
    public Task SendMessage()
    {
        var myMessage = new MyMessage();
        return messageSession.SendLocal(myMessage);
    }
}

Related Articles