相关疑难解决方法(0)

在.NET Core控制台应用程序中将app.config文件与NUnit3一起使用

环境:

我的解决方案中目前有三个项目:

  • .NET Standard 2.0库,其中包含一些我想测试的代码。
  • 一个.NET Core 2.2控制台应用程序,它引用该库以确保其正常工作。
  • 通过使用VS中的“ NUnit测试项目”模板创建的.NET Core 2.2控制台应用程序。

我的测试项目中的依赖项全部来自NuGet:

  • Moq版本=“ 4.10.1”
  • nunit“ Version =” 3.11.0“
  • NUnit.ConsoleRunner“ Version =” 3.10.0“
  • NUnit3TestAdapter“ Version =” 3.13.0“
  • Microsoft.NET.Test.Sdk“ Version =” 16.0.1“

问题:

.NET标准库依赖于任何使用它的应用程序中存在的app.config文件。它使用ConfigurationSectionConfigurationElement属性将值映射到类,与以下答案非常相似:具有嵌套集合的自定义配置节

.NET Core控制台应用程序中包含一个app.config文件,该库能够很好地解析出其中的值并使用它们。好极了。

另一方面,NUnit控制台应用程序具有相同的app.config文件,但是该库似乎看不到它。一旦尝试使用ConfigurationManager.GetSection("...")它读取值,就会返回null

有人在这样的环境中获得过app.config文件与NUnit3一起工作吗?


我尝试过的

看起来它支持配置文件,但我不知道,如果文档指的是一些特殊的NUnit的配置文件或app.config文件。

  • 我尝试将app.config重命名为my_test_project_name.dll.config
  • 我将配置文件“复制到输出目录”设置为“始终复制”
  • 我尝试了所有可能想到的类似名称(app.config,App.config,my_test_project_name.config,my_test_project_name.dll.config等)

在到目前为止编写的一个测试中,我还尝试了一些尝试,尝试以某种方式设置配置文件,例如使用建议AppDomain.CurrentDomain.SetData()(没有用,可能是因为NUnit3不支持AppDomain):

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\Path\To\My\Tests\my_test_project_name.dll.config");
Run Code Online (Sandbox Code Playgroud)

尽管NUnit存储库中有一些测试似乎表明可以在NUnit3中使用配置文件,但是该特定测试文件仅在.NET 4.5演示项目中引用,而不在.NET Core演示项目中引用。

c# nunit app-config nunit-3.0 .net-core

14
推荐指数
2
解决办法
794
查看次数

自定义ConfigurationSection

我使用IConfigurationSectionHandler接口来获取有关我的自定义配置部分的信息.但它已被弃用,我想改用ConfigurationSection.

如何使用此视图创建自定义ConfigurationSection并使用ConfigurationSection而不是IConfigurationSectionHandler:

<CustomSectionBlaBla>
   <Parent name="DB">
       <FirstChild value="someValue"/>
       <SecondChild value="someValue"/>
   <Parent/>
    ...
   <Parent name="UI">
       <FirstChild value="someValue"/>
       <SecondChild value="someValue"/>
   <Parent/>
<CustomSectionBlaBla/>
Run Code Online (Sandbox Code Playgroud)

.net c# configuration configurationsection

13
推荐指数
2
解决办法
8595
查看次数

App.config:自定义配置嵌套部分

我找到了自定义配置处理程序的一个很好的例子,并尝试将它用于我自己的实现.

我已经像这样设置了App.config:

<configSections>
  <section name="DocumentationSettings" type="ConfigHandler.DocumentationSettings,Settings"/>
</configSections>

<DocumentationSettings>
  <DocumentationSections>
    <DocumentationSection Id="AAA">
      <SectionDescription Value="SectionDescriptionAAA"/>
    </DocumentationSection>
    <DocumentationSection Id="BBB">
      <SectionDescription Value="SectionDescriptionBBB"/>
    </DocumentationSection>
    <DocumentationSection Id="CCC">
      <SectionDescription Value="SectionDescriptionCCC"/>
    </DocumentationSection>
  </DocumentationSections>
</DocumentationSettings>
Run Code Online (Sandbox Code Playgroud)

我使用此代码访问我的自定义配置:

DocumentationSettings documentationSettings = ConfigurationManager.GetSection("DocumentationSettings") as DocumentationSettings;

foreach (DocumentationSectionConfigElement section in (documentationSettings.DocumentationSections.Sections))
{
    Console.WriteLine(section.Id);
    Console.WriteLine(section.SectionDescription.Properties.Value);
}
Run Code Online (Sandbox Code Playgroud)

首先'Console.WriteLine'完美无缺.

所以我有以下问题和实施相关的问题:

  1. 第二个'Console.WriteLine'出错,错误:无法识别的属性'Value'.我已经放了"public SectionDescription SectionDescription",因为"DocumentationSectionConfigElement"类暴露了属性访问,但我可能错了,我先尝试将它放入"DocumentationSectionCollection",但我不知道如何在那里实现它,对我来说似乎是"DocumentationSectionCollection"仅实现"集合"逻辑.

  2. 我想要像这样访问"字段":

    section.Id section.SectionDescription.Value

或者像这样:

section.Properties.Id
section.SectionDescription.Properties.Value
Run Code Online (Sandbox Code Playgroud)

我看到"Collection"允许使用这样的索引器方法直接使用这些属性:

public DocumentationSectionConfigElement this[int index]
Run Code Online (Sandbox Code Playgroud)

但我不能在"SectionDescription"类上实现索引器方法,因为它是一个单独的部分而不是集合,所以当我访问字段时,这个"属性"名称仍然存在.

  1. 为了能够在这些配置对象上使用LINQ,我需要添加什么?我的意思是这样的:

    (documentationSettings.DocumentationSections.Sections).Select(x => x.Id)

  2. 复杂的XML结构配置处理程序是否有很好的例子?从我发现的那些大多数简单的结构,如:

但没有像这样的复杂结构的任何例子:

<section>
  <subSections>
    <subSection name="111">
      <Description Value="AAA"></Description>
      <Headers>
        <Header type="Main">
         <Properties>
           <Property name="Property1"/> …
Run Code Online (Sandbox Code Playgroud)

c# configuration app-config sections

4
推荐指数
1
解决办法
2346
查看次数