我有一堆DLL项目正在我的应用程序中,每个项目都包含自己的Settings.settings / app.config。当我编译应用程序并运行调试时,一切正常,但部署时间我无法让我的DLL读取自己的设置文件。
我一直在做一些阅读,很明显有几种方法可以让每个dll读取自己的配置 - 一种是将.dll.config专用于库,另一种是嵌入dll在process.exe.config中的配置。
我在尝试实施这些问题时遇到了重大问题,我想知道是否有人对此有任何好的文档 - 网络上似乎存在短缺。
如果可能的话,我想为每个库分别使用一个.dll.config,但是在一个紧要关头,让我的每个库读取他们自己的process.exe.config部分都可以。
任何人都可以指出我正确的方向,因为我已经非常接近推出这个应用程序,但这个绊脚石让我头疼。
编辑:当我合并配置文件时,我在使用我的库初始化对象时开始获取TypeInitializer异常。这可能只是我被推迟了,但有人有一个合并的配置文件和一些基本的演示代码的工作示例,用于从多个程序集中读取它吗?
答案 0 :(得分:2)
您遇到的“重大问题”是什么?我开始在exe的配置中嵌入dll的配置,这很有用,但很麻烦。我现在在一个dll项目中拥有所有配置内容。除了复制设置之外,我唯一需要做的就是将设置类更改为公开。
以下是合并的app.config的示例:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="SharedConfig.Client.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- Begin copy from library app.config -->
<section name="SharedConfig.Library.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- End copy from library app.config -->
</sectionGroup>
</configSections>
<applicationSettings>
<SharedConfig.Client.Properties.Settings>
<setting name="Bar" serializeAs="String">
<value>BarFromClient</value>
</setting>
</SharedConfig.Client.Properties.Settings>
<!-- Begin copy from library app.config -->
<SharedConfig.Library.Properties.Settings>
<setting name="Bar" serializeAs="String">
<value>BarFromLibrary</value>
</setting>
</SharedConfig.Library.Properties.Settings>
<!-- End copy from library app.config -->
</applicationSettings>
</configuration>
答案 1 :(得分:0)
让每个类库在自定义ConfigurationSection中定义配置设置。
然后将自定义节处理程序添加到process.exe.config文件中。
This MSDN article在解释中非常全面,在VB和C#中都有示例。
答案 2 :(得分:0)
见If app.config for a DLL should be in the "main config"… what do we do with WCF References in DLLs?。真正的答案是“复制和粘贴”。不幸的是,这是微软想到的一般解决方案。在某些情况下,可以使用.NET 2.0设置机制,因为它将默认值烘焙到DLL本身。在运行时,DLL可以将更新的设置保存到.exe.config。