NServiceBus uses the running process config file as its default source of configuration. Both the default behavior and the location from where the configuration is loaded can be changed. This can be done for the whole configuration or specific sections.
IProvideConfiguration
, IConfigurationSource
and CustomConfigurationSource
have been deprecated in Version 7. See the Version 7 upgrade guide for more information.Overriding an app.config section
A configuration for a specific section can be overridden using the IProvideConfiguration
interface. For example, rather than providing the RijndaelEncryptionServiceConfig
in app.config the code below provides an alternative configuration that will be used as long as RijndaelEncryptionServiceConfig
is found in the types scanned:
class CustomRijndaelEncryptionServiceConfigProvider :
IProvideConfiguration<RijndaelEncryptionServiceConfig>
{
public RijndaelEncryptionServiceConfig GetConfiguration()
{
return new RijndaelEncryptionServiceConfig
{
Key = "gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6"
};
}
}
Code-only configuration
If the endpoint must load its configuration from a source other than the 'app.config', an implementation of the IConfigurationSource
interface can be registered as a custom configuration source. This approach enables retrieving the configuration from any location, such as a database or a web service.
Registering custom configuration source
This code instructs NServiceBus to use MyCustomConfigurationSource
as a custom configuration source:
busConfiguration.CustomConfigurationSource(new MyCustomConfigurationSource());
Providing configuration from a custom location (other than app.config)
The GetConfiguration
method provides data for RijndaelEncryptionServiceConfig
directly in code, while allowing all other configuration sections to be retrieved from the config file.
public class MyCustomConfigurationSource :
IConfigurationSource
{
public T GetConfiguration<T>() where T : class, new()
{
if (typeof(T) == typeof(RijndaelEncryptionServiceConfig))
{
var config = new RijndaelEncryptionServiceConfig
{
Key = "gdDbqRpqdRbTs3mhdZh9qCaDaxJXl+e6"
};
return config as T;
}
// leave the rest of the configuration as is:
return ConfigurationManager.GetSection(typeof(T).Name) as T;
}
}