使用Enterprise Library在多个文件之间共享配置信息时遇到了一些问题。我有三个配置文件(app.config,dataConfiguration.config和exceptionHandlingConfiguration.config)。我正在使用.NET 4.0框架中的Windows 7计算机上的Enterprise Library 5.0和Visual Studio 2012。
在App.config中,我有以下
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</sectionGroup>
</configSections>
<appSettings>
</appSettings>
<enterpriseLibrary.ConfigurationSource selectedSource="SysConfig">
<sources>
<add name="SysConfig" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
/>
<add name="DataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
filePath="dataConfiguration.config" />
<add name="ExceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
filePath="exceptionHandlingConfiguration.config" />
</sources>
<redirectSections>
<add sourceName="DataConfiguration" name="dataConfiguration" />
<add sourceName="ExceptionHandling" name="exceptionHandling" />
</redirectSections>
</enterpriseLibrary.ConfigurationSource>
</configuration>
exceptionHandlingConfiguration.config包含
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<exceptionHandling>
<exceptionPolicies>
<add name="Policy">
<exceptionTypes>
<add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="NotifyRethrow" />
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
<validation />
<policyInjection>
<policies>
<add name="Application Policy">
<handlers>
<add type="Microsoft.Practices.EnterpriseLibrary.Validation.PolicyInjection.ValidationCallHandler, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
specificationSource="Both" name="Validation Call Handler" />
</handlers>
</add>
<add name="Global Policy">
<handlers>
<add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
exceptionPolicyName="Policy" name="Exception Handling Call Handler" />
</handlers>
</add>
</policies>
</policyInjection>
</configuration>
虽然dataConfiguration.config包含
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<dataConfiguration defaultDatabase="JDBDatabase" />
<connectionStrings>
<add name="JDBDatabase" connectionString="TestBookDB; server=Mononobe1423; Integrated Security=true;"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
我遇到的问题是当我运行代码时,收到错误消息
配置文件dataConfiguration.config没有root标签
如上所示,所有三个文件都包含配置标记。所以我迷失了错误是什么以及为什么我无法使用
中的任何一个访问其他文件中定义的部分对象var execonfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var db = execonfig.ConnectionStrings.ConnectionStrings["JDBDatabase"];
或
IConfigurationSource configSource = ConfigurationSourceFactory.Create();
var db = configSource.GetSection(DatabaseSettings.SectionName);
任何人都知道我错了什么?