最近在将原有代码迁移.NET Core, 代码的迁移基本很快,当然也遇到了不少坑,重构了不少,后续逐步总结分享给大家。今天总结分享一下ConfigurationManager遇到的一个问题。

先说一下场景:

迁移.NET Core后,已有的配置文件,我们希望做到兼容,比如说app.config和web.config,

这样配置文件尽可能地和.NET Framework是一套,尽可能低保持一致。比如:appSettings自定义configSection等等。

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <<strong>section</strong> name="CustomConfigs" type="ClassLibraryNetStandard.CustomConfigHandler, ClassLibraryNetStandard"/>

  </configSections>

  <<strong>CustomConfigs</strong>>

    <CustomConfig name="service1" order="0" reflectconfig="ClassLibraryNetStandard.TestService, ClassLibraryNetStandard"/>

    <CustomConfig name="service2" order="1" reflectconfig="ClassLibraryNetStandard.TestService2, ClassLibraryNetStandard"/>

  </CustomConfigs

  <<strong>appSettings</strong>>

    <add key="service" value="service1"/>

  </appSettings>

</configuration>

 对于上面配置读取我们做了以下几个事情

  1. 添加Nuget:System.Configuration.ConfigurationManager

   2. 保证原有自定义Section配置相关的代码、读取配置的代码,迁移到.NET Core后编译通过

   3. 修改配置文件、单元测试

 一、添加Nuget:System.Configuration.ConfigurationManager

搜索System.Configuration.ConfigurationManager:找到Nuget包,并添加引用:

二、保证原有自定义Section配置相关的代码、读取配置的代码,迁移到.NET Core后编译通过

示例代码中,自定义配置类CustomConfig

using System;

using System.Collections.Generic;

using System.Text;

namespace ClassLibraryNetStandard

{

    public class CustomConfig

    {

        public string Name { getset; }

        public string ReflectConfig { getset; }

        public int Order { getset; }

    }

}

同时对应的Section配置节解析类:CustomConfigHandler,实现接口:System.Configuration.IConfigurationSectionHandler

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

namespace ClassLibraryNetStandard

{

   public class CustomConfigHandler : System.Configuration.IConfigurationSectionHandler

    {

        public object Create(object parent, object configContext, XmlNode section)

        {

            var configs = new List<CustomConfig>();

            foreach (XmlNode childNode in section.ChildNodes)

            {

                string name = null;

                var config = new CustomConfig();

                if (childNode.Attributes["name"] != null)

                {

                    name = childNode.Attributes["name"].Value;

                    config.Name = name;

                    if (childNode.Attributes["order"] != null)

                    {

                        config.Order = Convert.ToInt32(childNode.Attributes["order"].Value);

                    }

                    if (childNode.Attributes["reflectconfig"] != null)

                    {

                        config.ReflectConfig = childNode.Attributes["reflectconfig"].Value;

                    }                 

                    configs.Add(config);

                }

            }

            return configs;

        }

    }

}

同时,我们编写了一个简单的配置管理类:CustomConfigManager,其中有配置读取方法,直接读取配置文件:

1

public static List<CustomConfig> GetCustomConfigs()

{

    var sectionConfig = System.Configuration.ConfigurationManager.GetSection("CustomConfigs");

    if (sectionConfig != null)

    {

        return  sectionConfig as List<CustomConfig>;

    }

    return null;

}

  这里我们使用了.NET Standard 2.0 library project,代码编译通过:

1>------ 已启动全部重新生成: 项目: ClassLibraryNetStandard, 配置: Debug Any CPU ------
1>C:\Program Files\dotnet\sdk\3.0.100-preview3-010431\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(151,5): message NETSDK1057: 你正在使用 .NET Core 的预览版。请查看 https://aka.ms/dotnet-core-preview
1>ClassLibraryNetStandard -> C:\Users\***\source\repos\NETFrameworkTest\ClassLibraryNetStandard\bin\Debug\netstandard2.0\ClassLibraryNetStandard.dll
========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0 个 ==========

 三、修改配置文件、单元测试

添加MSTest单元测试工程:

增加App.config配置文件:

在单元测试方法中测试配置的读取:

[TestMethod]

 public void ConfigTest()

 {

     var configs = ClassLibraryNetStandard.CustomConfigManager.GetCustomConfigs();

     Assert.IsNotNull(configs);

 }

  原本以为,肯定可以获取到配置,实际获取的configs是null。

换了个Console类的应用,同样的配置文件读取,一点没有问题:

对比看了一下这两个工程,发现除了实际编译生成的配置文件名称不同,其他都一样。

问题肯定出在了单元测试工程上。Google了一下:有以下发现:

1

MSTest is running as testhost.dll, which means that ConfigurationManager is reading settings from testhost.dll.config when executing under .NET core. <br>It will look for testhost.dll.config where the testhost.dll is located as the accepted answer states. <br>What is not mentioned is that it will also look for testhost.dll.config in the location where you have your test dlls.

  一句话:MSTest以testhost.dll运行,去取的配置文件是testhost.dll.config

这太尴尬了,直接无语,不过有两个解决方案:

1. 直接在单元测试工程中将app.config文件改为:testhost.dll.config

2. 修改单元测试工程文件,配置编译后事件,动态copy生成testhost.dll.config

试过之后,果真可以了,问题解决,分享个大家。

原文链接:https://www.cnblogs.com/tianqing/p/11514840.html


.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com

.NetCore技术研究-ConfigurationManager在单元测试下的坑相关推荐

  1. 云计算环境下安全关键技术研究

    摘 要 云计算已发展成为大数据应用.跨平台应用的主要解决方案,而虚拟化.大规模.开放性等特征,带来了更多安全威胁和挑战,通过分析云计算安全防御模型架构,分别对云计算安全的技术特征.运行特征.保障模式等 ...

  2. 写好测试,提升应用质量。涨薪分分钟!!!(二)之单元测试下开发模式、技术框架选择

    目录: 四. 单元测试下开发模式.技术框架选择 单元测试是按照测试范围来划分的.TDD.BDD 是按照开发模式来划分的.因此就有各种排列组合,这里我们只关心单元测试下的 TDD.BDD 方案. 在单元 ...

  3. 面向对象的类测试技术研究

    面向对象的类测试技术研究 摘要:类是面向对象软件的基本构成单元,类测试是面向对象软件测试的关键.从基于服务的.基于对象动态测试模型的.基于流图的以及基于规约的四个方面论述了类测试的思想和方法. 关键词 ...

  4. 邮件安全隐患及其防范技术研究

    邮件安全隐患及其防范技术研究<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" ...

  5. 提高建模效率,改变手工作坊式生产,AutoML的技术研究与应用进展如何了?

    整理 | 王银 出品 | AI科技大本营(ID:rgznai100) [导读]12 月 5-7 日,由中国计算机学会主办,CCF 大数据专家委员会承办,CSDN.中科天玑协办的中国大数据技术大会(BD ...

  6. [公告]欢迎您加入WF技术研究团队

    Microsoft Windows Workflow Foundation (WWF) 是一个可扩展框架,用于在 Windows 平台上开发工作流解决方案.Windows Workflow Found ...

  7. 【博士论文】物联网数据安全可信的共享技术研究

    来源:专知 本文约2000字,建议阅读5分钟 本文为你分享物联网数据安全可信的共享技术研究. 来自上海交通大学牛超越的博士论文,入选2021年度"CCF优秀博士学位论文奖"初评名单 ...

  8. Linux技术研究-基础篇(raid与LVM,配额)

    Linux技术研究-基础篇(raid与LVM,配额) 创建RAID-5 若想建立新的md1设备 只在/dev下建立还不够 重启后会消失 固化的方法是 为了使udev自动产生/dev/md1, /dev ...

  9. 《模拟信息转换器(AIC)的实现技术研究》读书笔记

    此论文的出现十分有必要,由于压缩感知要处理的信号是有限维的离散信号,并且是可以压缩(可稀疏表示)的离散信号,若以奈奎斯特速率采样模拟信号得到离散信号,再以压感的框架去处理,这如何体现出压感的优势呢? ...

最新文章

  1. Anaconda:包安装以XGBoost为例
  2. gperf工具的使用
  3. 三维重建:重定位问题
  4. 8.1 文件查找local;find使用
  5. JS/PHP中,数组与字符串的转换,这次总算是记住了
  6. ansible-handlers
  7. 用Python中的tkinter模块作图
  8. 风压和功率计算公式轴流式_这是你见过最全的风机计算公式
  9. 封装制作ghost xp,含加入域帐号配置迁移脚本。
  10. vue单元测试vue test utils使用初探
  11. win10运行窗口打开共享服务器很慢,win10局域网共享文件慢怎么办 局域网共享文件夹无法访问是什么原因...
  12. MapReduce: 大规模集群上的简化数据处理
  13. Nginx从入门到精通(笔记)
  14. 免费的在线Web文件管理器:Net2FTP,Pydio,eXtplorer,KodExplorer–功能强大
  15. airtest获取设备号和获取设备宽度、高度、绝对坐标 相对坐标、滑动屏幕
  16. 前端css样式如何设置内边框
  17. 解决 win10 桌面 资源管理器未响应
  18. fsleyes -- 一款多功能影像数据查看器
  19. Express WEB 应用开发框架-姜威-专题视频课程
  20. 51nod_1265 四点共面

热门文章

  1. 让批处理文件(.bat)程序无窗口(隐藏/静默)运行
  2. COM组件与.NET技术对比
  3. head rush ajax chapter4 DOM
  4. 用计算机算算术平方根顺序是ON然后是什么,第2课时用计算器求一个正数的算术平方根.ppt...
  5. 将Teams app升级到net6
  6. 如何使计算机为您读取文档
  7. 愚蠢的怪胎技巧:通过命令行管理SkyDrive
  8. 汉克尔变换matlab,HankelTransform
  9. 这一新的可视化方法教你优雅地探索相关性
  10. Sonnedix收购意大利11.2MW光伏电站产品组合