NServiceBus Host Profiles customization

Component: NServiceBus Host
NuGet Package NServiceBus.Host (5.x)
Target NServiceBus Version: 5.x

Custom profile

In certain scenarios it might be useful to define additional profiles. For example, some organizations have a few testing or staging environments that might require different configurations. To define a custom Profile create a class that implements the NServiceBus.IProfile marker interface:

namespace YourNamespace
{
    public class YourProfile :
        NServiceBus.IProfile { }
}

Note that the profile definition does not contain any configuration. The actual configuration for the profile is provided in profile behaviors.

Profile behaviors

Profile behaviors specify configuration for a given profile. They are created by implementing NServiceBus.Hosting.Profiles.IHandleProfile<T>, where T is the specific profile type:

class IntegrationProfileHandler : IHandleProfile<Integration>
{
   public void ProfileActivated(EndpointConfiguration config)
   {
      config.EnableInstallers();
   }
}

Profile behaviors might be defined using multiple classes for the same profile, or using a single class for multiple profiles.

Profile behaviors might also be used to customize configuration of a specific element, for example an email component might have the following requirements:

  • Production profile: use an SMTP server
  • Integration profile: write emails to disk
  • Lite profile: do nothing

That can be achieved with the following implementation:

class LiteEmailBehavior :
    IHandleProfile<NServiceBus.Lite>
{
    public void ProfileActivated(BusConfiguration busConfiguration)
    {
        // set the NullEmailSender in the container
    }

    public void ProfileActivated(Configure config)
    {
    }
}

class IntegrationEmailBehavior :
    IHandleProfile<NServiceBus.Integration>
{
    public void ProfileActivated(BusConfiguration busConfiguration)
    {
        // set the FileEmailSender in the container
    }

    public void ProfileActivated(Configure config)
    {
    }
}

class ProductionEmailBehavior :
    IHandleProfile<NServiceBus.Production>
{
    public void ProfileActivated(BusConfiguration busConfiguration)
    {
        // set the SmtpEmailSender in the container
    }

    public void ProfileActivated(Configure config)
    {
    }
}

NServiceBus will find the provided behaviors for the email component at runtime and invoke only methods appropriate for the profile that it's currently using. As a result, for each environment a different implementation of the email component will be registered with the DI container and used in the system.

Profile vs endpoint configuration

In some situations profile behavior implementation might depend on the endpoint configuration.

For example, NServiceBus Host uses this information to configure publishers. Endpoints that don't publish messages don't need to have any subscription storage. The Lite profile configures an in-memory subscription storage, but the Integration and Production profiles should use configuration specified in the endpoint configuration, such as RavenDB or NHibernate.

Endpoint's configuration can be accessed to customize profile behaviors in the following way:

class MyProfileHandler :
    IHandleProfile<MyProfile>
{
    public void ProfileActivated(BusConfiguration busConfiguration)
    {
        // set something else in the container
    }

    public void ProfileActivated(Configure config)
    {
    }
}

Last modified