Property injection

Component: NServiceBus
NuGet Package NServiceBus (5.x)
Standard support for version 5.x of NServiceBus has expired. For more information see our Support Policy.

NServiceBus will automatically enable property injection for known types across the supported dependency injection libraries. Use the Func overload of .ConfigureComponent to get full control over the injected properties if needed.

A common use case is to set primitive properties on message handlers. Given the below handler:

public class EmailHandler :
    public string SmtpAddress { get; set; }
    public int SmtpPort { get; set; }

    public void Handle(EmailMessage message)
        using (var client = new SmtpClient(SmtpAddress, SmtpPort))
            // use client

Setting the properties is done as follows:

    registration: components =>
        var component = components.ConfigureComponent<EmailHandler>(DependencyLifecycle.InstancePerUnitOfWork);
        component.ConfigureProperty(handler => handler.SmtpAddress, "");
        component.ConfigureProperty(handler => handler.SmtpPort, 25);

Handler/Saga specific API

Versions 5.2 and above supported a new, more explicit, API that specifically targets Handlers and Sagas.

This API has been obsoleted with error in Versions 6 and above.
busConfiguration.InitializeHandlerProperty<EmailHandler>("SmtpAddress", "");
busConfiguration.InitializeHandlerProperty<EmailHandler>("SmtpPort", 25);

Last modified