当前位置: 移动技术网 > IT编程>开发语言>c# > .Net Standard(.Net Core)实现获取配置信息

.Net Standard(.Net Core)实现获取配置信息

2019年09月22日  | 移动技术网IT编程  | 我要评论

一、前言

在.net framework框架有专门获取webconfig配置的方法供我们使用,但是在.net core或者.net standard中没有可以直接使用的方法来获取配置文件信息,下面就来实现获取配置信息。

二、获取配置信息的实现

在.net core中,他的配置信息的载体是一个json文件,我们现在就计划所有项目(包含.net framework和.net standard(.net core)框架)都是json文件作为配置的载体。

首先通过nuget加载如下的包:

install-package microsoft.extensions.configuration
install-package microsoft.extensions.configuration.json
install-package microsoft.extensions.dependencyinjection
install-package microsoft.extensions.options
install-package microsoft.extensions.options.configurationextensions

现在我们使用json配置文件的内容有一下格式:

{
  "connectionstrings": {
    "cxyorder": "server=laptop-aqul6mde\\mssqlservers;database=cxyorder;user id=sa;password=123456;trusted_connection=false;"
  },
  "appsettings": {
    "systemname": "pdf .net core",
    "date": "2017-07-23",
    "author": "pdf"
  },
  "serviceurl": "https://www.baidu.com/getnews"
}

创建pftconfiguration.cs文件,代码如下:

 public class pftconfiguration
    {
        /// <summary>
        /// pftconfiguration.configuration.getconnectionstring("cxyorder"); 
        /// pftconfiguration.configuration["serviceurl"];
        /// pftconfiguration.configuration["appsettings:systemname"];
        /// </summary>
        public static iconfiguration configuration { get; set; }
        static pftconfiguration()
        {
            configuration = new configurationbuilder()
            .add(new jsonconfigurationsource { path = "appsettings.json", reloadonchange = true })
            .build();
        }

        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="path">json文件类型</param>
        /// <typeparam name="t">返回实体类型</typeparam>
        /// <param name="key">json关键字</param>
        /// <returns></returns>
        public static t getappsettings<t>(string key, string path) where t : class, new()
        {
            try
            {
                if (string.isnullorwhitespace(path))
                {
                    throw new exception("解析配置错误,配置文件路径为空");
                }
                if (string.isnullorwhitespace(key))
                {
                    throw new exception("解析配置错误,配置key为空");
                }
                var config = new configurationbuilder()
                    .setbasepath(appdomain.currentdomain.basedirectory)
                    .add(new jsonconfigurationsource { path = path, reloadonchange = true })
                    .build();
                var appconfig = new servicecollection()
                    .addoptions()
                    .configure<t>(config.getsection(key))
                    .buildserviceprovider()
                    .getservice<ioptions<t>>()
                    .value;
                return appconfig;
            }
            catch (exception ex)
            {
                throw new exception("解析配置(对象)异常", ex);
            }

        }


        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="key">json关键字</param>
        /// <param name="path">json文件类型</param>
        /// <returns></returns>
        public static string getappsettings(string key, string path)
        {
            try
            {
                if (string.isnullorwhitespace(path))
                {
                    throw new exception("解析配置错误,配置文件路径为空");
                }
                if (string.isnullorwhitespace(key))
                {
                    throw new exception("解析配置错误,配置key为空");
                }
                var config = new configurationbuilder()
                    .setbasepath(appdomain.currentdomain.basedirectory)
                    .add(new jsonconfigurationsource { path = path, reloadonchange = true, optional = false })
                    .build();
                var appconfig = config.getsection(key).value;
                return appconfig;
            }
            catch (exception ex)
            {
                throw new exception("解析配置(字符串)异常", ex);
            }
        }


    }
}

里面定义了3种获取方法

1、pftconfiguration.configuration["appsettings:systemname"] 文件路径默认为appsettings.json,然后通过节点来获取,这个方式有点类似于.net framework中获取配置的方法

2、pftconfiguration.getappsettings<t>(string key, string path),他是获取指定配置文件,指定节点,以t对象的形式返回节点内容

3、pftconfiguration.getappsettings(string key, string path),他是获取指定配置文件,指定节点,以字符串的形式返回节点内容

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网