views:

113

answers:

4

Possible Duplicate:
Reading dll.config (not app.config!) from a plugin module.

I have two different projects, say A and B. I need to use some classes of A in B. So I added a reference to A in B. When I tried to run the application, I stated getting Object reference set to null exception. On investigation, I found that when I access classes of A from B, control goes to project A, but C# still uses config file of project B instead of using project A's config file. How do I get around this? How can I "include" A's config file in the dll?

I have gone through this blog but I feel it is a very dirty way of doing it. There ought to be an easier way!

Let me know if the question is unclear..

+3  A: 

I believe .NET will always load the app.config file associated with the application rather than any libraries. There are complicated ways of specifying your own locations for config files - or just using your own configuration framework instead of the built-in one - but I don't think you can just ask .NET to load a config file per DLL.

Jon Skeet
hmm.. I just ended up copying the relevant keys from the other config file.. there were just 2 keys here so it was ok. But I wonder how large applications run when there can be conflicts between keys..
Raze2dust
@Raze2dust: The newer style of configuration settings have separate config sections, so there shouldn't be much chance of conflict.
Jon Skeet
Thanks, can you point me to some resource where I can learn more about using my own configuration framework?
Raze2dust
@Raze2dust: Not really - because the point is it would be your own one! It could be as simple or complicated as you want, or you could use a third-party framework (e.g. Spring).
Jon Skeet
+1  A: 

As I understand this you want integrate a app.config into your dll. Check this out:

http://stackoverflow.com/questions/2441858/how-do-you-load-the-app-config-file-into-a-dll

MUG4N
Isn't that defeating the object of having a config file in the first place? Config files are meant to allow configuration changes to an application without the need to recompile or even at runtime in some cases
fletcher
Yeah but config files always config a project not several projects
MUG4N
+1  A: 

You can copy the relevant sections of A's config into the B config file and it'll work properly, but it's a bit tedious to say the least. I suppose you could automate it with a custom tool though.

Mark H
+1  A: 

Why don't you just all the necessary settings into the config file from project B? If you use some tools and libraries from an external vendor you just do the same stuff to configure it.

If you abstracted the configuration good enough you should be fine. Using another configuration file than the default one - well i would consider this as bad practice.

Yves M.