开发者

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

I have a .NET 4 class library project, which is used by multiple web projects. In this开发者_如何学C class library I need to get a DB connection string and it needs to be the same for all the web projects. Currently I've got it as a setting in each web.config file, but this is not ideal. Is there any way I can have that configuration stored with the DLL, but still allow it to be modified at runtime (ie. hardcoding the connection string is out)?

App.config seems to be generally ignored for a DLL, even though it does get renamed to assemblyname.dll.config and copied to the bin directory for the web. I tried making it an "application setting" (ie. using the auto-generated class derived from System.Configuration.ApplicationSettingsBase) and this appeared to work, but changing the value in the DLL .config file at runtime had no effect, so I suspect it's really just using the hardcoded default value of the setting.


You can store the setting in the Registry Database if it should be the same for all programs on the machine using your library.


Personally, I would do that (even if you somehow manage to get it to work, and yes the built-in configuration mechanism won't let you). I would always store behavioral settings of an application with the application's own configuration. I have done and seen systems where such values were stored in a database - which is of course not very feasible for a connection string.

A DLL as such is passive (or should be) and only acts upon explicit calls from its users, and thus should not need any configuration for its own purpose (except may "global" diagnostics stuff, but even then...), as the caller can provide it when or before using the respective functionality.

If you have multiple applications that incidentally require the same settings, then this is nothing the DLL should know or even care about.

To make things more manageable, you might consider relocating this issue into the build or deployment process instead. In your application configuration files (web.config files for that matter) put a placeholder for the connectionstring. Then during build or deployment (whatever suits your needs better) replace that placeholder with the true connection string, which you can keep in a single file.


There is no way to change this. This is just how .NET works. You have to copy the settings from the DLL app.config to the web.config or programName.exe.config file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜