一、前言
在.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),他是获取指定配置文件,指定节点,以字符串的形式返回节点内容
原文地址:https://www.cnblogs.com/snailblog/p/11565907.html
时间: 2024-10-28 21:18:10