NServiceBus – Altering the behaviour of profiles at runtime

NServiceBus supports three profiles out of the box, Lite, Integration and Production. Dependent on which profile you have set at startup, NServiceBus will run with a different setup, i.e. writing to memory in Lite or a database in production.

Regardless of which profile you are running the same functionality is expected. This is where custom profiles come in. Custom profiles enables the ability to alter segments of functionality based on the profile you are running in. This is achieved by creating a class that implements the IHandleProfile interface, where T is either NServiceBus.Lite, NServiceBus.Integration, or NServiceBus.Production. For example

class LiteIntegrationPointBehavior : IHandleProfile<NServiceBus.Lite>
{
    public void ProfileActivated()
    {
        // return a stub response
    }
}

class ProductionIntegrationPointBehavior : IHandleProfile<NServiceBus.Production>
{
    public void ProfileActivated()
    {
        // call the real service
    }
}

An area where this feature would prove invaluable is during testing. A project I am currently working on interacts with a number of third party integration points for the system to operate end to end. This causes problems for our integration test suite, which are executed as part of the nightly build. We do not want to have to rely on a third party’s system provide a testing interface or even for the service to be available, to ensure our tests all go green. So rather than sending a message to an endpoint, we can simply configure our injected profile to not call the service and return a fake valid response. This means we can still test are core code, don’t have to build stub services and we also would not need to change the config of the standard setup for the sake of the test suite.

Read more about profiles on the NServiceBus website