views:

1565

answers:

3

My understanding is, that .NET doesn't really 'do' config files for DLLs - only the main Executable or Web App gets a config file, and all DLLs referenced by the Executable/Web App read from that.

But in VS2008, if you add a Web Reference to a Class Library (DLL) project, it adds a Settings.Settings file and an app.config file to the project. They contain the main URL for the Web Reference.

So what are these files for? There's no way for the DLL to read them unassisted, right?

edit: the contents of the app.config and Settings.Settings seem to make a difference though: changing (for example) the Web Reference URL in the DLL Project's app.config file on its own makes no difference, but if you edit the URL in the app.config and then open the Settings.Settings file, the changes you made in app.config then get copied into Settings.Settings. And then, the DLL picks up the new value at run time. How is it doing this?

edit: Part of my confusion here is because I'm not too clear on the difference between Settings.Settings and app.config and how they relate to each other, so maybe people can help out with that issue too.

+1  A: 

There's no way for the DLL to read them, but they're strong hints to a consumer of your DLL of what they might want to include in the real Settings/Config file

Edit

In response to the comment by OP - Whatever is last edited in the settings gets compiled into the code as a default to take if no setting of the correct name is present at runtime. So that's why that's working.

Damien_The_Unbeliever
Changes to the app.config get copied to the Settings.Settings file and then seem to get picked up at run time, so its not just for show. See my edit above
codeulike
Ah, thanks. I wasn't too clear on the way app.config relates to Settings.Settings so your edit helps.
codeulike
+1  A: 

Visual Studio has to add this stuff somewhere and it doesn't know which application you want to put it in. You can access the config for the DLL by doing the following:

var config = ConfigurationManager.OpenExeConfiguration("MyDll.dll.config");

The only time I've found this useful is when I wrote a plugin as a DLL for a 3rd party application and wanted my DLL to be configurable (not something most people do that often I suspect).

Usually though you will just move the config parts you need into your app.config or web.config.

Edit- In regards to your update that makes sense. Settings.settings application scoped settings come from the application's app.config file. For application scoped settings its really just a strongly typed class representing these settings.

RichardOD
So DLLs can have Settings.Settings files and read them at runtime? Where do the contents of those files end up after a build?
codeulike
No, the DLL with a Settings.Settings file will read it from the app.config/web.config. If that can't be found it will fall back to its defaults.
RichardOD
I think I am confusing you, with my use of the term app.config. I mean the main application's configuration file (MyApp.exe.config or Web.config). To me the DLL's config is really MyDll.dll.config. HTH
RichardOD
I was also confused about what happens to values in Settings.Settings. As I now understand it, they get 'baked' into the Assembly as defaults to use if no values come from config files?
codeulike
Yes, exactly that. Look inside the Settings.Designer file that gets generated. You will see an attribute DefaultSettingValueAttribute, like this- [global::System.Configuration.DefaultSettingValueAttribute("Test")]. You can read more about this attribute here- http://msdn.microsoft.com/en-us/library/system.configuration.defaultsettingvalueattribute.aspx
RichardOD
A: 

There is a possibility to read from config files in DLL. Just add an app.config file to the DLL project, and make sure you read the configuration settings from inside the DLL. When deployed, your config file needs to have the name "MyDLL.dll.config" (assuming your DLL is named "MyDLL.dll") and be in the same folder as the DLL.

The following code should return my connectionstring from my dll :

return ConfigurationManager.AppSettings["ConnectionString"];

ZokiManas
That won't work.
RichardOD
Also, thats not what I'm asking. I'm not asking how to make a config file work with a DLL, I'm asking why Visual Studio is bothering to add one for a DLL project.
codeulike