Custom profile
In certain scenarios, it is useful to define additional host profiles, for example when there are several testing and staging environments with different configurations. To define a custom profile, create a class that implements the NServiceBus.
interface:
namespace YourNamespace
{
public class YourProfile :
NServiceBus.IProfile { }
}
Note that the profile definition does not contain any configuration. The 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.
, where T
is the specific profile type:
class IntegrationProfileHandler : IHandleProfile<Integration>
{
public void ProfileActivated(EndpointConfiguration config)
{
config.EnableInstallers();
}
}
Profile behaviors can be defined using multiple classes for the same profile, or using a single class for multiple profiles.
Profile behaviors can also be used to customize configuration of a specific element, for example, an email component with 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(EndpointConfiguration endpointConfiguration)
{
// set the NullEmailSender in dependency injection
}
}
class IntegrationEmailBehavior :
IHandleProfile<NServiceBus.Integration>
{
public void ProfileActivated(EndpointConfiguration endpointConfiguration)
{
// set the FileEmailSender in dependency injection
}
}
class ProductionEmailBehavior :
IHandleProfile<NServiceBus.Production>
{
public void ProfileActivated(EndpointConfiguration endpointConfiguration)
{
// set the SmtpEmailSender in dependency injection
}
}
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 dependency injection 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 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 configuration can be accessed to customize profile behaviors as follows:
class MyProfileHandler :
IHandleProfile<MyProfile>
{
public void ProfileActivated(EndpointConfiguration endpointConfiguration)
{
// set something else in dependency injection
}
}