存在 Web 参考时的 .NET DLL 设置和配置 - 发生了什么?

发布于 2024-08-08 15:16:00 字数 575 浏览 10 评论 0原文

我的理解是,.NET 并没有真正为 DLL '做' 配置文件 - 只有主可执行文件或 Web 应用程序获取配置文件,并且可执行文件/Web 应用程序引用的所有 DLL 都从中读取。

但在 VS2008 中,如果将 Web 引用添加到类库 (DLL) 项目,则会向该项目添加一个 Settings.Settings 文件和一个 app.config 文件。它们包含 Web 参考的主 URL。

那么这些文件有什么用呢? DLL 无法在没有帮助的情况下读取它们,对吗?

编辑:app.config 和 Settings.Settings 的内容似乎有所不同:更改(例如)DLL 项目的 app.config 文件中的 Web 引用 URL 本身没有任何区别,但如果您编辑 URL在 app.config 中,然后打开 Settings.Settings 文件,您在 app.config 中所做的更改将被复制到 Settings.Settings 中。然后,DLL 在运行时获取新值。它是如何做到这一点的?

编辑:我的部分困惑是因为我不太清楚 Settings.Settings 和 app.config 之间的区别以及它们之间的相互关系,所以也许人们也可以帮助解决这个问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独自唱情﹋歌 2024-08-15 15:16:01

Visual Studio 必须在某个地方添加这些东西,并且它不知道您想要将其放入哪个应用程序。您可以通过执行以下操作来访问 DLL 的配置:

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

我唯一一次发现它有用是当我编写了插件作为第 3 方应用程序的 DLL,并希望我的 DLL 是可配置的(我怀疑大多数人经常这样做)。

通常,您只需将所需的配置部分移至 app.config 或 web.config 中。

编辑 - 关于您的有意义的更新。 Settings.settings 应用程序范围的设置来自应用程序的 app.config 文件。对于应用程序范围的设置,它实际上只是代表这些设置的强类型类。

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.

又爬满兰若 2024-08-15 15:16:01

可以从 DLL 中的配置文件中读取。
只需将 app.config 文件添加到 DLL 项目,并确保从 DLL 内部读取配置设置。部署时,您的配置文件需要具有名称“MyDLL.dll.config”(假设您的 DLL 名为“MyDLL.dll”)并且与 DLL 位于同一文件夹中。

以下代码应从我的 dll 返回我的连接字符串:

return ConfigurationManager.AppSettings["ConnectionString"];

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"];

奈何桥上唱咆哮 2024-08-15 15:16:00

DLL 无法读取它们,但它们强烈提示 DLL 的使用者他们可能希望在真实的设置/配置文件中包含什么内容

编辑

OP - 如果运行时不存在正确名称的设置,则设置中最后编辑的内容都会编译到代码中作为默认值。这就是为什么它有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文