单元测试项目无法读取应用程序的App.config文件

7
在运行Visual Studio Pro 2013 12.0.21005.1的构建机上,我有一个单元测试项目无法正确读取其App.config文件(如果有的话)。以下代码返回null:
System.Configuration.ConfigurationManager.GetSection("loggingConfiguration")
不仅是日志配置部分无法被读取,任何尝试读取任何部分都会导致值为null。
在我运行Visual Studio Pro 2013 12.0.31101.00 Update 4的笔记本电脑上,单元测试项目可以很好地读取App.config,并且上面的调用返回预期的对象。
我的XML如下所示:
<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
    <section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>

<loggingConfiguration name="" tracingEnabled="false" defaultCategory="Verbose" logWarningsWhenNoCategoriesMatch="false">
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" source="MyCompany" formatter="Text Formatter" log="Application" />
      <add name="Rolling Flat File Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" fileName="Service.log" formatter="Text Formatter" rollFileExistsBehavior="Increment" rollInterval="Day" />
      <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" type="MyCompany.Core.Instrumentation.ConsoleTraceListener, MyCompany.Core" formatter="Debug Formatter" traceOutputOptions="None" name="Debug Console Listener" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp(local)}{newline}&#xA;Title: {title}{newline}&#xA;Message: {message}{newline}&#xA;Severity: {severity}{newline}&#xA;Priority: {priority}{newline}&#xA;Categories: {category}{newline}&#xA;EventId: {eventid}{newline}&#xA;Machine: {localMachine}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}" name="Text Formatter" />
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="{timestamp(local:FixedFormatUSDate)} {timestamp(local:FixedFormatTime)} [{severity} P{priority} T{win32ThreadId}/'{threadName}']   {title}: {message} [{category}]" name="Debug Formatter" />
    </formatters>
    <logFilters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.CategoryFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" categoryFilterMode="AllowAllExceptDenied" name="Category Filter">
        <categoryFilters>
        </categoryFilters>
      </add>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" maximumPriority="4" name="Priority Filter" />
    </logFilters>
    <categorySources>
      <add switchValue="Information" name="MyCompany.Core.Configuration">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="ServiceExceptionPolicy">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Critical">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Error">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Warning">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Information">
        <listeners>
          <add name="Event Log Listener" />
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Verbose">
        <listeners>
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="Off" name="All Events" />
      <notProcessed switchValue="Off" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Rolling Flat File Listener" />
          <add name="Debug Console Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="ServiceExceptionPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" logCategory="ServiceExceptionPolicy" eventId="100" severity="Error" title="Enterprise Library Exception Handling" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" priority="0" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
  <dataConfiguration defaultDatabase="SOMEDATABASE" />
  <connectionStrings>
  <!-- some connection strings -->
  </connectionStrings>
</configuration>

有人知道这个在一个环境中失败而在另一个环境中没有失败的原因吗?

听起来你的本地测试是在构建目录中运行,但是构建机器正在部署文件夹中运行测试,并且配置文件没有被部署。 - Randy Levy
我检查了,.config文件和所有的DLL都在该在的地方。 - MiloDC
1
我进一步研究了这个问题。显然,偶尔无法识别App.config是MSTest已知的问题——这是一个过时的测试运行程序(自2010年或2011年以来),我们的持续集成显然仍在使用它。目前,我打算忽略相关的测试,直到我们用VSTest替换CI管道中的MSTest(我已经要求负责人进行更新)。 - MiloDC
啊...VS 2013。它有太多问题了,我放弃使用它了。用2012或2015吧。我记得2013有很多问题。某些项目无法良好运行等。 - T.S.
你是否尝试使用另一个部分检索来检查你的设置?例如:System.Configuration.ConfigurationManager.GetSection("exceptionHandling") - Peter Csala
1个回答

0

这需要自定义配置:

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />

看看type里面指向的自定义配置处理程序。我猜测试项目中缺少DLL引用。


谢谢。自定义配置文件在哪个文件中? - Kirsten

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接