views:

307

answers:

2

When I try and use code that makes use of the Enterprise Library Caching Block I get the following error:

The "cachingConfiguration" section is not available in the supplied configuration source.

The section is in my app.config file for that particular assembly though. If I copy the file into the unit test assembly that makes use of the afore mentioned code everything works. Is there any way that I can force it to use the app config that exists in the referenced library so I don't need to duplicate it in every assembly that makes use of it?

+1  A: 

The dll config file that you see in the IDE (if you are using the designer to add settings etc) is largely for convenience. The runtime won't look for it; it wants the file from yourexename.config. Some components provide the facility to specify a separate config file path - I don't know if this is the case for entlib.

Alternatively if you are spawning your own AppDomains you can specify the config file path. And finally some config sections can be referenced in from other files rather than using the file completely (see configSource here) - but in general (and especially for tests) it is easier to just copy the configuration section into the top-level app's config file (or the unit test's config file in this case).

Marc Gravell
I was hoping there would be something similar to Properties.Settings.Default.xxxx where the value seems to get 'baked' into the assembly, unless you specify it higher in the config hierarchy. It sounds like this is not the case for config sections?
Brehtt
+2  A: 

Yes.

Select an app.config file to be your master config file (a good choice would be the one in the project of the main application executable).

Now go to your other project (fx. the unit test project). Right-click and select Add Existing Item. Point to the master app.config file and add using the "As link" option:

Add as link

Now you only need to manage one app.config file, and the other projects will 'reference' this file.

Option 2: If you wan't to control what config file the Enterprise Library uses for caching configuration, use:

var fileSource = new FileConfigurationSource(configFilePath);
var factory = new CacheManagerFactory(fileSource);
ICacheManager manager = factory.CreateDefault();
peter_raven
Thanks, I ended up using the second option and reading in the file path as Properties.Settings.Default.SomePath. This way the assembly works out of the box but config files higher up can override the cache config location to somewhere else.
Brehtt