Solutions    |    Downloads    |    License    |    Documentation    |    Training    |    Support    |    Customers    |    About Us

Customizing NServiceBus Configuration

Last Updated: Jan 14, 2013 03:24AM IST
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.

Overriding App.Config section

Using IProvideConfiguration<T> model of overriding config, is the preferred way of overriding a specific section.
The following is an example taken from the PubSub sample:  

namespace Subscriber1
{
    using NServiceBus.Config;
    using NServiceBus.Config.ConfigurationSource;

    //demonstrate how to override specific configuration sections
    class ConfigOverride : IProvideConfiguration<MessageForwardingInCaseOfFaultConfig>
    {
        public MessageForwardingInCaseOfFaultConfig GetConfiguration()
        {
            return new MessageForwardingInCaseOfFaultConfig
                       {
                           ErrorQueue = "error"
                       };
        }
    }
}

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.
 

About NServiceBus    |    Contact Us    |    Privacy    |    Follow us on:   
Copyright 2010-2013 NServiceBus. All rights reserved
NSB_Y@yahoo.com
http://assets2.desk.com/r1046ffeaa2233e531563a32d7edef6677d8a78b5/javascripts/
nservicebus
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
true
Invalid characters found
/customer/portal/articles/autocomplete
There was an error contacting Get Satisfaction
View All
0
discussions
replies
Questions
Ideas
Problems
Praise