当前位置: 首页 > news >正文

刘淼 网站开发网站建设 后期维护

刘淼 网站开发,网站建设 后期维护,天河做网站开发,免费网站模板建站最近在将原有代码迁移.NET Core, 代码的迁移基本很快#xff0c;当然也遇到了不少坑#xff0c;重构了不少#xff0c;后续逐步总结分享给大家。今天总结分享一下ConfigurationManager遇到的一个问题。先说一下场景#xff1a;迁移.NET Core后#xff0c;已有的配置文件当然也遇到了不少坑重构了不少后续逐步总结分享给大家。今天总结分享一下ConfigurationManager遇到的一个问题。先说一下场景   迁移.NET Core后已有的配置文件我们希望做到兼容比如说app.config和web.config,   这样配置文件尽可能地和.NET Framework是一套尽可能低保持一致。比如appSettings、自定义configSection等等。?xml version1.0 encodingutf-8 ?configuration  configSections    strongsection/strong nameCustomConfigs typeClassLibraryNetStandard.CustomConfigHandler, ClassLibraryNetStandard/  /configSections  strongCustomConfigs/strong    CustomConfig nameservice1 order0 reflectconfigClassLibraryNetStandard.TestService, ClassLibraryNetStandard/    CustomConfig nameservice2 order1 reflectconfigClassLibraryNetStandard.TestService2, ClassLibraryNetStandard/  /CustomConfigs   strongappSettings/strong    add keyservice valueservice1/  /appSettings/configuration 对于上面配置读取我们做了以下几个事情   1. 添加NugetSystem.Configuration.ConfigurationManager   2. 保证原有自定义Section配置相关的代码、读取配置的代码迁移到.NET Core后编译通过   3. 修改配置文件、单元测试 一、添加NugetSystem.Configuration.ConfigurationManager   搜索System.Configuration.ConfigurationManager找到Nuget包并添加引用   二、保证原有自定义Section配置相关的代码、读取配置的代码迁移到.NET Core后编译通过  示例代码中自定义配置类CustomConfigusing System;using System.Collections.Generic;using System.Text;namespace ClassLibraryNetStandard{    public class CustomConfig    {        public string Name { get; set; }        public string ReflectConfig { get; set; }        public int Order { get; set; }    }}  同时对应的Section配置节解析类CustomConfigHandler实现接口System.Configuration.IConfigurationSectionHandlerusing 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 ListCustomConfig();            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其中有配置读取方法直接读取配置文件1public static ListCustomConfig GetCustomConfigs(){    var sectionConfig System.Configuration.ConfigurationManager.GetSection(CustomConfigs);    if (sectionConfig ! null)    {        return  sectionConfig as ListCustomConfig;    }    return null;}  这里我们使用了.NET Standard 2.0 library project代码编译通过1------ 已启动全部重新生成: 项目: ClassLibraryNetStandard, 配置: Debug Any CPU ------1C:\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-preview1ClassLibraryNetStandard - 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了一下有以下发现       1MSTest is running as testhost.dll, which means that ConfigurationManager is reading settings from testhost.dll.config when executing under .NET core. brIt will look for testhost.dll.config where the testhost.dll is located as the accepted answer states. brWhat 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
http://wiki.neutronadmin.com/news/75870/

相关文章:

  • 成都网站建设与网站制作wordpress 主题 后门
  • 网站开发管理招聘湖州猪八戒做网站
  • 公司专业网站建设vs做的网站图片显示不了
  • 免费创建网站培训类 网站后台
  • php做商品网站动漫网站html
  • iis6.0做网站压缩wordpress建立公司网站
  • 网站开发教案优化 导航网站
  • 滑县网站建设服务常用搜索网站
  • 甘露园网站建设wordpress添加电台
  • 专题探索网站开发模式特点外贸seo搜索优化
  • 几种语言的网站如何做在线制作图片旋转动态
  • dw免费网站模板下载做T恤卖网站
  • 网站推广方法主要有哪几种时代创信网站设计 北京
  • 网站 功能呢html5电影网站模板
  • 互联网招聘网站排行营销网站建设服务
  • 为什么网站上传照片传不上去网站商城建设公司
  • 布谷 海南网站建设网站 宣传册
  • 品牌营销策划网站做广告推广哪个平台好
  • 制作制作网站建设的wordpress 分类图像描述
  • dz网站如何搬家建筑人才招聘哪个网站最好
  • 网站设计的目标是什么网站文章伪原创如何做
  • 免费建站赚钱wordpress整站打包
  • 网站注册域名查询网站做的漂亮的企业
  • 网站打不开是怎么回事手机购物网站模板下载
  • 银川做网站推广创业网站怎么做
  • 东莞制作公司网站会展网站代码源码
  • 美妆网站建设规划企业网络部署方案
  • 长春网站建设哪家好网站排名优化学习
  • 电子商务网站建设参考文献2018无锡网站建设服务
  • tklink的登录做网站为什么在百度搜不到我的网站