Store configuration settings for .NET DLL (not EXE or Web)

I have a .NET 4 class library project that is used by several web projects. In this class library, I need to get the database connection string, and it should be the same for all web projects. I currently have a parameter in every web.config file , but that is not ideal. Can I have such a configuration stored in a DLL, but still allow it to be changed at runtime (i.e., hard-code the connection string)?

App.config is usually ignored for the DLL, although it is renamed assemblyname.dll.config and is copied to the bin directory for the Internet. I tried to make it "install the application" (i.e., using an automatically generated class derived from System.Configuration.ApplicationSettingsBase ), and this seems to work, but changing the value in the DLL.config file at run time had no effect. so I suspect really just using the hard-coded default value for the parameter.

+4
source share
3 answers

You can save this parameter in the registry database if it is the same for all programs on the computer using your library.

+2
source

Personally, I would do it (even if you manage to get it to work, and yes, the built-in configuration mechanism will not allow you). I will always store the behavioral settings of the application with its own application configuration. I did and saw systems where such values โ€‹โ€‹were stored in the database, which, of course, is not very possible for the connection string.

The DLL as such is passive (or should be) and acts only on explicit calls from its users and, therefore, should not need any configuration for its own purpose (with the exception of the "global" diagnostic tools, but even then ... ), because the caller can provide it when or before using the appropriate features.

If you have several applications that, by the way, require the same settings, then this is not what the DLL should know or even care.

To make things more manageable, you might consider porting this problem to the build or deployment process. In application configuration files (for example, web.config files), place a placeholder for the connection string. Then during build or deployment (which suits your needs best), replace this placeholder with a true connection string that you can save in a single file.

+2
source

Unable to change this. This is how .NET works. You need to copy the settings from the app.config dll to the web.config file or programName.exe.config.

0
source

Source: https://habr.com/ru/post/1336078/


All Articles