Property injection

Component: NServiceBus | Nuget: NServiceBus (Version: 5.x)

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

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

Edit
public class EmailHandler :
    IHandleMessages<EmailMessage>
{
    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:

Edit
busConfiguration.RegisterComponents(
    registration: components =>
    {
        var component = components.ConfigureComponent<EmailHandler>(DependencyLifecycle.InstancePerUnitOfWork);
        component.ConfigureProperty(handler => handler.SmtpAddress, "10.0.1.233");
        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.
Edit
busConfiguration.InitializeHandlerProperty<EmailHandler>("SmtpAddress", "10.0.1.233");
busConfiguration.InitializeHandlerProperty<EmailHandler>("SmtpPort", 25);

Last modified