.NET 3.5 DLL 使用自己的配置文件
我需要 .NET 3.5 DLL 有自己的配置文件。该 DLL 可以从许多不同的应用程序调用,因此存储在配置文件中的信息(如连接字符串)需要保存在 DLL 可以引用的配置文件中。我想要的是当使用DLL时,我需要将用于引用信息的配置文件“切换”为DLL配置文件。然后,当 DLL 使用完配置信息后,切换回默认状态。该DLL是使用.NET 3.5编写的。我一直在寻找如何做到这一点,我一直在寻找如何将信息与 exe 的 app.config 文件合并。就我而言,我不知道如何在何处使用此 DLL 来修改任何 exe 的 app.config 文件。该解决方案需要独立。但是,我用于创建 DLL(包含业务对象)的基类期望在配置文件中查找连接字符串和其他信息,因此这就是为什么我需要在它出现时“切换”到我的 DLL 配置文件。被访问,然后将其切换回来,这样我就不会弄乱调用 DLL 的 exe 应用程序。
I need to have a .NET 3.5 DLL have its own config file. This DLL could be called from many different apps so the information (like connection strings) stored in the config file needed to be kept in a config file that the DLL can reference. What I want is when the DLL is used, I need to "switch" the config file that is used to reference information to be the DLLs config file. Then when the DLL is done with using the configuration information, the switch is made back to the default one. The DLL is written using .NET 3.5. I have been searching for how to do this and what I keep finding is how to merge information with an exe's app.config file. In my case, I don't know how where this DLL will be used to modify any exe's app.config files out there. This solution needs to be stand alone. However, my base classes used to create the DLL (which contain business objects) are expecting to lookup the connection string and other information in a config file so that is why I need to "switch" to my DLL config file at the time when it is accessed and then switch it back so I don't mess up the exe app that called the DLL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
.NET 2.0 及更高版本的配置系统为您提供了多种功能 - 例如,您可以按需加载特定的配置文件。这需要更多的工作 - 但它确实有效。
您必须执行以下操作:
您还需要确保正在构建类库 DLL 的配置并将其复制到可以访问它的位置。最坏的情况是,您需要指定一个特定的配置文件,并通过在 Visual Studio 属性窗口中设置其“复制到输出目录”属性来确保将其复制到输出目录。
您还应该查看 CodeProject 上 Jon Rista 关于 .NET 2.0 配置的三部分系列。
强烈推荐,写得很好,非常有帮助!
马克
The .NET 2.0 and up configuration system gives you capabilities - you can e.g. load a specific config file on demand. It's a bit more work - but it works.
You'd have to do something like this:
You also need to make sure that the config for the class library DLL is being built and copied to a location where you can get at it. Worst case you need to specify a specific config file and make sure it gets copied to the output directory by setting its "Copy To Output Directory" property in the Visual Studio properties window.
You should also check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.
Highly recommended, well written and extremely helpful!
Marc
通常,当您在应用程序级别有设置时,您需要在应用程序级别生成这些设置,然后将它们渗透到您的库中。它在初始化中添加了几行代码以注入依赖项,但无论如何您本质上都是在尝试这样做。
如果您尝试在库的每次部署中并排包含仅 dll 的配置文件,那么您将遇到比其价值更多的问题,而且它在语义上没有多大意义。
以 System.Data 为例...当您创建连接时,您指定连接字符串,而不是仅为连接字符串创建单独的配置文件以与 System.Data 库并排部署。 [这会导致大量问题,因为它可能驻留在您系统上的 GAC 中]
Generally when you have settings at the application level, you need to generate those settings at the application level, and then percolate them through your libraries. It adds a few more lines of code to your initialization to inject dependencies, but you are essentially trying to do that anyway.
You are going to run into way more problems than its worth if you try to include a config file for just the dll side by side with every deployment of your library, and it just doesn't make a lot of sense semantically.
Take System.Data for example...when you create a connection, you specify the connection string, not create a separate config file for just the connection string to deploy side by side with the System.Data library. [and that would cause tons of problems since it likely resides in the GAC on your system anyway]
您不能使用内置的 .Net 配置系统来执行此操作。它被设计(理应如此)用于配置应用程序进程,而不是单个 Dll。正确的方法是将 dll 的配置设置添加到“使用”(引用)该 dll 的每个可执行应用程序的 app.config 系统中。 (或在 machine.config 中)
所有使用 dll 的应用程序
可以通过放置它们来完成
在 Machine.config 中(在
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG
对于 .Net2.0
如果您不想这样做,那么您将必须编写自己的代码来打开、读取和写入自定义 Xml 文件(或您选择使用的任何格式)以提取那些设置。
You cannot use the built-in .Net configuration system to do this. It is designed (as it should be ) to configure application processes, not indvidual Dlls. The correct way to go about this is to add the configuration settings for the dll into the app.config system for each executable application that "uses" (has a reference to) that dll. (or in the machine.config)
to ALL applictions that uset he dll
can be accomplished by putting them
in the Machine.config (in
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG
for .Net2.0
If you don't want to do it that way, then you will have to write your own code to open, read, and write frm a custom Xml file, (or whatever format you choose to use) to extyract those settings.
最简单的答案是将这些值放入 machine.config 中。这样,所有使用 dll 的应用程序都将能够通过 .net 应用程序配置类访问信息。通过这种方式,如果您绝对需要覆盖某个应用程序的值,则可以在主应用程序的 app.config 文件中覆盖它。
The easiest answer to this would be to put the values in the machine.config. That way all apps using the dll will be able to access the information through the .net app configuration classes. With this way too, if you absolutely need to override a value for a certain applicatin, you can override it in the main application's app.config file.
我知道这篇文章有点旧,但我想投入 2 美分。虽然在可能的情况下,我同意应用程序应该提供设置,而不是 dll。我发现一种情况,虽然 dll 特定的配置似乎更理想。
我们使用 Quartz.NET 的调度/任务应用程序。我们开发了一个接口来创建和监控 Quartz 服务器的作业。 Quartz.NET 的一大优点是,您可以创建“作业”程序并将它们编译成 DLL,然后将它们放入 Quartz.NET 服务器文件夹中,通过反射等,Quartz 就能够开始使用新的 DLL无需对 Quartz 服务器进行任何额外的调整...如果配置文件中没有需要保存的信息。
对于 Quartz 服务器,如果您有任何应保存在配置文件中的特定配置信息,则必须将其放入 Quartz.NET 配置文件中。我们创建了几个“作业”应用程序,每个应用程序都有自己的配置信息,因此现在我们的 Quartz.NET 配置文件中充满了所有这些不相关的设置。拥有特定于 dll 的配置文件将大大减少 Quartz.NET 服务器的混乱。我看到的唯一其他选项是将所有这些单独的设置添加到设置数据库表中,但为了合并和维护代码,出于我们的目的,单独的配置文件是更好的选择。
只是把它扔在那里思考一下。
I know this post is a little old, but I wanted to throw my 2 cents in. While in may cases, I would agree that the application should supply the settings, not the dll. I have found one situation in while dll specific configs seems to be more desireable.
We use the scheduling/task applicaiton Quartz.NET. We developed a interface to create and monitor jobs against the Quartz server. One of the great things about Quartz.NET is that you create your "job" programs and compile them into DLLs and then just drop them in the Quartz.NET server folder and through reflection and such, Quartz is able to start using the new DLL without any additional tweaks to Quartz server...if there is no information that needs to be held in config files.
With Quartz server if you have any specific config information that should be kept in a config file, you have to put it in the Quartz.NET config file. We have created several "job" applications, each with their own bits of config information, so now our Quartz.NET config file is littered with all these unrelated settings. Having a dll specific config file would greatly reduce the clutter of the Quartz.NET server. The only other option I see is to add all these individual settings into a settings DB table, but for consolidating and maintenance of the code, for our purposes, individual config files are preferable.
Just throwing that out there to think about.