IConfigurationSource.GetConfiguration called multiple times for the same type


(Ismael Hamed) #1

NServiceBus 5.2.24

We have a custom IConfigurationSource that retrieves the endpoint mappings from the database:

public class CustomConfiguration : IConfigurationSource
    public T GetConfiguration<T>() where T : class, new()
        if (typeof(T) == typeof(UnicastBusConfig))
            // Retrieve mappings from database
            // ...
            return new UnicastBusConfig { MessageEndpointMappings = mappings } as T;

        return ConfigurationManager.GetSection(typeof(T).Name) as T;

But we’ve noticed that GetConfiguration() gets called multiple times for type UnicastBusConfig during the endpoint initialization, thus querying the database and reconstructing the object every time. If this cannot be avoided, is there a way to cache the mappings instead?

(David Boike) #2

@ismaelhamed you’re right, it’s not cached. If the implementation is expensive (i.e. your database call) then you’ll need to cache the result yourself.