问题描述
我有一个名为Application
的C#项目,该项目基本上被认为代表了最顶层的应用程序层.所有其他层均被视为ClassLibraries,例如BUSINESS
,DAO
和UTIL
.现在,我希望使用 App.config 文件来配置应用程序.为此,我需要使该配置文件对于所引用的ClassLibraries(程序集)可见.
I have a C# Project called Application
which is basically considered to represent the top most application layer. All other Layers are treated as ClassLibraries like BUSINESS
, DAO
and UTIL
. Now I want that the application be configurable by using the App.config file. For that purpose I need to make this configuration file visible for the referenced ClassLibraries (Assemblies).
对我来说,最合适的解决方案是UTIL
程序集可以访问App.config并能够将这些可访问项共享到上层应用程序层.
For me, the most suitable solution would be that the UTIL
assembly has access to the App.config and is able to share those accessible items to the upper application layers.
到目前为止,我尝试在UTIL
程序集中创建 Settings.settings 文件,该文件定义了一项:Name: ElemName; Type: String; Scope: Application
.位于Application
程序集中的 App.config 文件包含以下来源:
So far I tried to create a Settings.settings file in the UTIL
assembly which defines one item: Name: ElemName; Type: String; Scope: Application
. The App.config file which is located within the Application
assembly contains the following source:
<applicationSettings>
<UTIL.Settings>
<setting name="ElemName" serializesAs="String">
<value>SomeValue</value>
</setting>
</UTIL.Settings>
</applicationSettings>
如果我现在尝试通过以下方式访问此属性:Settings.Default.ElemName
,构建失败,并显示错误:'UTIL.Settings'由于其保护级别而无法访问
If I now try to access this property via: Settings.Default.ElemName
,the build fails with error: 'UTIL.Settings' is inaccessible due to its protection level
另一种方法,通过程序集UTIL
A further approach, to make this property visible via a helper class inside assembly UTIL
public String GetElemName()
{
return Settings.Default.ElemName;
}
失败,并且在System.Configuration.dll中发生了'System.Configuration.ConfigurationErrorsException'... ...附加信息:配置系统无法初始化
fails with an: 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll ... Additional information: Configuration system failed to initialize
我如何使其运行? (我只需要阅读配置)
How can I get it running? (I only need to read the configuration)
推荐答案
我以另一种方式解决了这个问题.
I solved this issue in a different way.
名为Application
的项目仍包含App.config文件.所有引用的程序集(Business
,DAO
,UTIL
和TestAssemblies)仅共享应用程序配置.
The Project called Application
still contains the App.config file. All referenced assemblies (Business
, DAO
, UTIL
, and TestAssemblies) just share the applications configuration.
如何共享配置:
- 项目(
Business
,DAO
,UTIL
等)->添加->现有项目->'选择'App.config->添加为链接 - 项目->添加参考-> .NET-> System.Configuration->确定
-
string configValue = ConfigurationManager.AppSettings["token"];
- Project (
Business
,DAO
,UTIL
, etc.) -> Add -> Existing Item -> 'Select' App.config -> Add As Link - Project -> Add Reference -> .NET -> System.Configuration -> OK
string configValue = ConfigurationManager.AppSettings["token"];
App.config文件的外观:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
...
<appSettings>
<add key="token" value="ConfigValue" />
</appSettings>
...
</configuration>
如果没有对现有App.conifg文件的引用,则在使用NUnit运行时不会解析该值.如果应用程序以调试模式启动,即使没有用户 Maarten
这篇关于如何使被引用的ClassLibraires(Assebmlies)的App.config值可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!