C#net6控制台程序中读取appsettings.json
背景
我们知道,在原来.netframework平台下的控制台程序配置文件为App.config,微软推出跨平台net6平台后,配置文件改为了appsettings.json,不论是开发控制台应用还是web应用都可以用appsettings.json作为配置文件。在net6下开发web应用,可以直接通过注入的方式读取appsettings.json中的配置项,那么在控制台应用下如何读取呢。
具体实现
首先是添加NuGet包依赖,主要依赖一下3个包
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.FileExtensions
- Microsoft.Extensions.Configuration.Json
然后我们可以写一个工具类,如下: ConfigurationHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
namespace Wongoing.Basic
{
public class ConfigurationHelper
{
#region 字段定义
private static readonly string CONFIGFILELOCK = "CONFIGFILELOCK";
private static readonly string APPSETTINGS = "AppSettings";
private static readonly string PROSETTINGS = "ProSettings";
private static IConfigurationRoot configurationRoot = null;
#endregion
#region 单例实现
private static ConfigurationHelper _instance = null;
private ConfigurationHelper()
{
configurationRoot = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory()))
.AddJsonFile("appsettings.json", optional: false)
.Build();
}
public static ConfigurationHelper Instance
{
get
{
lock(CONFIGFILELOCK)
{
if (_instance == null)
{
_instance = new ConfigurationHelper();
}
return _instance;
}
}
}
#endregion
#region GetConnectionString
public string GetConnectionString(string key, string defaultValue)
{
string result = configurationRoot.GetConnectionString(key);
if (result == null)
{
return defaultValue;
}
else
{
return result;
}
}
#endregion
#region GetAppSetting
public string GetAppSetting(string key, string defaultValue)
{
string result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(APPSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
result = configurationSection.GetSection(key).Value;
}
}
return result;
}
public int GetAppSetting(string key, int defaultValue)
{
int result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(APPSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
string strResult = configurationSection.GetSection(key).Value;
int.TryParse(strResult, out result);
}
}
return result;
}
public bool GetAppSetting(string key, bool defaultValue)
{
bool result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(APPSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
string strResult = configurationSection.GetSection(key).Value;
bool.TryParse(strResult, out result);
}
}
return result;
}
public double GetAppSetting(string key, double defaultValue)
{
double result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(APPSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
string strResult = configurationSection.GetSection(key).Value;
double.TryParse(strResult, out result);
}
}
return result;
}
#endregion
#region GetProSetting
public string GetProSetting(string key, string defaultValue)
{
string result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(PROSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
result = configurationSection.GetSection(key).Value;
}
}
return result;
}
public int GetProSetting(string key, int defaultValue)
{
int result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(PROSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
string strResult = configurationSection.GetSection(key).Value;
int.TryParse(strResult, out result);
}
}
return result;
}
public bool GetProSetting(string key, bool defaultValue)
{
bool result = defaultValue;
IConfigurationSection configurationSection = configurationRoot.GetSection(APPSETTINGS);
if (configurationSection != null)
{
if (configurationSection.GetSection(key) != null)
{
string strResult = configurationSection.GetSection(key).Value;
bool.TryParse(strResult, out result);
}
}
return result;
}
#endregion
}
}
使用方法
appsettings.json内容实例如下:
{
"ConnectionStrings": {
"DefaultConnection": "Server=10.10.80.15;Port=3306;DataBase=wongoing-dcs;User Id=root;Password=root@123;Allow User Variables=True;Connection Timeout=15"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ProSettings": {
"NavTheme": "dark",
"Layout": "side",
"ContentWidth": "Fluid",
"FixedHeader": true,
"FixSiderbar": true,
"Title": "工业数采",
"PrimaryColor": "daybreak",
"ColorWeak": false,
"SplitMenus": false,
"HeaderRender": true,
"FooterRender": true,
"MenuRender": true,
"MenuHeaderRender": true,
"HeaderHeight": 48
}
}
public static readonly string Title = Wongoing.Basic.ConfigurationHelper.Instance.GetProSetting("Title", "工业数采");
public static readonly string DefaultConnection = Wongoing.Basic.ConfigurationHelper.Instance.GetConnectionString("DefaultConnection", "Server=localhost;DataBase=wongoing-dcs;User Id=root;Password=wongoing@123;Allow User Variables=True;Connection Timeout=15");
|