Profile picture Schedule a Meeting
c a n d l a n d . n e t

Overriding NServiceBus Configuration

Dusty Candland | |

I needed to override the default NServiceBus configuration (app.config/web.config). This turns out to be really easy with the 2.0 NServiceBus release.

First, when setting up the Bus I needed to call CustomConfigurationSource(myCustomSource) like this:

var unicastBusConfig = configure.CastleWindsorBuilder(container) .CustomConfigurationSource(myCustomSource) .XmlSerializer("http://tempuri.com") .MsmqTransport() .PurgeOnStartup(purgeOnStartup) .UnicastBus();

Second, I needed to implement IConfigurationSource on my myCustomSource class. This default implementation takes the passed in type and creates the configuration section handler of that type. To override a specific configuration section you can create an instance of that type, set it’s properties and return it. Otherwise, create and return the class without overriding any values.

There are four types of configuration sections used in NServiceBus currently, MsmqSubscriptionStorageConfig, DBSubscriptionStorageConfig, MsmqTransportConfig, and UnicastBusConfig.

public T GetConfiguration() where T : class { if (typeof(T) == typeof(MsmqSubscriptionStorageConfig)) return ConfigurationManager.GetSection(typeof(T).Name) as T;

if (typeof(T) == typeof(DBSubscriptionStorageConfig)) return ConfigurationManager.GetSection(typeof(T).Name) as T;

if (typeof(T) == typeof(MsmqTransportConfig)) return MsmqTransportConfiguration() as T;

if (typeof(T) == typeof(UnicastBusConfig)) return UnicastBusConfiguration() as T;

return null; }

Finally, the MsmqTransportConfiguration method creates a instance of MsmqTransportConfig and sets it’s values. In this case to fields that are passed in to the constructor, but it could be some other strategy or custom settings class.

private MsmqTransportConfig MsmqTransportConfiguration() { return new MsmqTransportConfig { ErrorQueue = errorQueue, InputQueue = inputQueue, MaxRetries = maxRetries, NumberOfWorkerThreads = numberOfWorkerThreads }; }

Webmentions

These are webmentions via the IndieWeb and webmention.io. Mention this post from your site: