Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET DLL Settings and Config when there's a Web Reference - whats going on?

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.

like image 372
codeulike Avatar asked Oct 21 '09 08:10

codeulike


People also ask

Why DLL config is created?

The reason dll. config exists at all is because VS doesn't distinguish between libraries and EXEs when it needs to generate config settings. Therefore if you happen to be inside a library and VS needs to generate a config entry it'll generate it in a dll. config but that file will never be used at runtime.

Can DLL have config file?

Using configuration files in DLL is not trivial, but is very simple. Using configuration files in DLL is not trivial. To accomplish this, we must follow these steps: Add a reference to System.

Does app config get compiled into DLL?

If you don't want to expose values, make sure you have an app. config with deployment values (empty, 0, or something). The values WILL be compiled into the DLL as default values.

Where are. net application Settings stored?

Settings. settings is located in the My Project folder for Visual Basic projects and in the Properties folder for Visual C# projects. The Project Designer then searches for other settings files in the project's root folder. Therefore, you should put your custom settings file there.


2 Answers

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.

like image 91
Damien_The_Unbeliever Avatar answered Nov 16 '22 01:11

Damien_The_Unbeliever


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.

like image 25
RichardOD Avatar answered Nov 16 '22 00:11

RichardOD