Property injection

Component: NServiceBus
NuGet Package NServiceBus (6.x)

NServiceBus will automatically enable property injection 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 it to set primitive properties on message handlers. Given then below handler:

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

    public Task Handle(EmailMessage message, IMessageHandlerContext context)
        using (var client = new SmtpClient(SmtpAddress, SmtpPort))
            // use client
        return Task.CompletedTask;

Setting the properties is done as follows:

    registration: components =>
            builder =>
                return new EmailHandler
                    SmtpPort = 25,
                    SmtpAddress = ""
            dependencyLifecycle: DependencyLifecycle.InstancePerUnitOfWork);

Last modified