Property injection

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

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:

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:

var components = configure.Configurer;
components.ConfigureProperty<EmailHandler>(handler => handler.SmtpAddress, "10.0.1.233");
components.ConfigureProperty<EmailHandler>(handler => handler.SmtpPort, 25);

Last modified