NServiceBus Home

Contents

  • Long-Running Processes
  •     Sagas

Customizing NServiceBus Configuration

Customization

NServiceBus uses the process' config file as its default source of configuration. The pluggability and extensibility of NServiceBus allow you to change many of its behaviors, including where it gets its configuration from. This can be done across all of NServiceBus or you can choose which part of NServiceBus should get its configuration from some other source.

Overriding in code when hosting NServiceBus yourself

If you need to override only a single value of a configuration section and other than that want the configuration section to remain in the config file, that can be done using the RunCustomAction method to hook into the initialization process of NServiceBus as follows:

NServiceBus.Configure.With()
  .Log4Net()
  .DefaultBuilder()
  .XmlSerializer()
  .MsmqTransport()
     .IsTransactional(false)
     .PurgeOnStartup(false)
  .UnicastBus()
     .ImpersonateSender(false)
  .RunCustomAction(() => 
     Configure.Instance.Configurer.ConfigureProperty<MsmqTransport>(mt => mt.Address, "someQueue")
   )
  .CreateBus()
  .Start();

In this example, we can see the use of the static Instance property on the Configure class to get access to the current configuration object in NServiceBus. The Configurer property provides an object that allows us to add or override configuration, and the ConfigureProperty method allows us to override a specific property of a specific type providing a custom value.

The above code is setting the Address property of the MsmqTransport to "someQueue". This will override the InputQueue property of the MsmqTransportConfig section in the config file.

Overriding in code with the NServiceBus host

When using the NServiceBus Host, we can hook into the initialization process by implementing IWantCustomInitialization and including in there the calls that were made in the RunCustomAction method above:

class MsmqTransportConfigOverride : IWantCustomInitialization
{
  public void Init()
  {
    Configure.Instance.Configurer.ConfigureProperty<MsmqTransport>(mt => mt.Address, "someQueue");
  }
}

As you can see, the code which does the overriding is the same, it just how you inject it into the initialization of NServiceBus that differs depending on your use of the host process.

Replacing App.Config

If you don't want your process to have its configuration specified in the config file, you can write a class which implements IConfigurationSource and in it retrieve configuration from any location you like - a database, a web service, anything. Here's how that's done:

NServiceBus.Configure.With()
  .CustomConfigurationSource(new MyConfigSource())
  ... // rest of initialization code


public class MyConfigSource : IConfigurationSource
{
  public T GetConfiguration<T>() where T : class
  {
    // the part you are overriding
    if (typeof(T) == typeof(MsmqTransportConfig))
      return new MsmqTransportConfig {InputQueue = "someQueue", /* other values */ } as T;

    // leaving the rest of the configuration as is:
    return ConfigurationManager.GetSection(typeof(T).Name) as T;
  }
}

In the example above, in our initialization code we're instructing NServiceBus to use a CustomConfigurationSource and we're passing in an instance of an object we wrote - MyConfigSource. In its GetConfiguration method we're providing data for MsmqTransportConfig directly in code, while allowing all other configuration sections to be retrieved from the config file.

IMPORTANT: You'll need to add a reference to System.Configuration to use the ConfigurationManager object.

In order to do this when using the NServiceBus host, you'll need to implement IWantCustomInitialization but this time on the class implementing IConfigureThisEndpoint as described here.

Legal

“NServiceBus” is among the trademarks of NServiceBus Ltd. All other product and company names and marks mentioned are the property of their respective owners and are mentioned for identification purposes only.

© 2010-2012 NServiceBus Ltd. All rights reserved.