ASP.NET Core与配置系统的集成

目录

配置系统

默认添加的配置提供者

加载命令行中的配置。

运行环境

读取方法

[User Secrets](#User Secrets)

注意事项

Zack.AnyDBConfigProvider

案例


配置系统

默认添加的配置提供者

  1. 加载现有的IConfiguration。
  2. 加载项目根目录下的appsettings.json。
  3. 加载项目根目录下的appsettings.{Environment}.json。
  4. 当程序运行在开发环境下,程序会加载"用户机密"配置。
  5. 加载环境变量中的配置。

加载命令行中的配置。

运行环境

ASP.NET Core 会从环境变量中读取名字为ASPNETCORE_ENVIRONMENT的值。推荐值:Development(开发环境)、Staging(测试环境)、Production(生产环境)。

读取方法

app.Environment.EnvironmentName、app.Environment.IsDevelopment()......

cs 复制代码
[HttpGet]
public string Test()
{
    return Environment.GetEnvironmentVariable("haha");
}
cs 复制代码
Program
app.Environment.IsDevelopment()

Controller
private readonly IWebHostEnvironment webHostEnvironment;

[HttpGet]
public string Test()
{
    return webHostEnvironment.EnvironmentName;
}

User Secrets

  1. 把不方便放到appsettings.json中的机密信息放到一个不在项目中的json文件中。
  2. 在ASP.NET Core项目上单击鼠标右键,选择【管理用户机密】。
  3. secrets.json文件到底保存在哪里?如何和项目建立关系?csproj文件中的<UserSecretsId>

注意事项

  1. 供开发人员使用的,不适合在生产环境中使用。
  2. 仍然是明文存储。不想别人看到怎么办?Azure Key Vault、Zack.AnyDBConfigProvider等。无法完全避免。加强安全防控更重要。
  3. 如果因为重装、新员工等原因导致secrets.json重建,就要重新配置,麻烦。如果影响大的话,还是用集中式配置服务器。

Zack.AnyDBConfigProvider

https://github.com/yangzhongke/Zack.AnyDBConfigProviderhttps://github.com/yangzhongke/Zack.AnyDBConfigProvider

案例

  1. 系统的主要配置(Redis、Smtp)放到配置专用的数据库中。Zack.AnyDBConfigProvider
  2. 连接配置数据库的连接字符串配置在"用户机密"中。"Data Source=.;Initial Catalog=demo1;Integrated Security=SSPI;"
  3. 把Smtp的配置显示到界面上。
  4. 程序启动的时候就连接Redis,并且把Redis连接对象注册到依赖注入系统中。
cs 复制代码
secrets.json:
{
  "connStr": "Data Source=.;Initial Catalog=demo1;Integrated Security=SSPI;TrustServerCertificate=true;"
}

public record SmtpSettings()
{
    public string Server { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
}

program.cs:
//从数据库动态加载配置
builder.Host.ConfigureAppConfiguration((hostCtx, configBuilder) =>
{
    //配置中读取名为"ConnStr"的连接字符串
    string connStr = builder.Configuration.GetSection("ConnStr").Value;
    //添加数据库配置源
    configBuilder.AddDbConfiguration(() => new SqlConnection(connStr), reloadOnChange: true, reloadInterval: TimeSpan.FromSeconds(2));
});
builder.Services.Configure<SmtpSettings>(builder.Configuration.GetSection("Smtp"));
//添加Redis配置源
builder.Services.AddSingleton<IConnectionMultiplexer>(sp =>
{
    //在Program.cs中读取配置的一种方法
    string constr = builder.Configuration.GetSection("Redis").Value;
    return ConnectionMultiplexer.Connect(constr);
});

Controller:
private readonly IOptionsSnapshot<SmtpSettings> optSmtp;
private readonly IConnectionMultiplexer connectionMultiplexer;

public Test(IOptionsSnapshot<SmtpSettings> optSmtp, IConnectionMultiplexer connectionMultiplexer)
{
    this.optSmtp = optSmtp;
    this.connectionMultiplexer = connectionMultiplexer;
}

[HttpGet]
public string Demo1()
{
    var ping = connectionMultiplexer.GetDatabase(0).Ping();
    return optSmtp.Value.ToString() + ":" + ping;
}
相关推荐
weixin_379880928 天前
.Net Core WebApi集成Swagger
java·服务器·.netcore
The Future is mine10 天前
.Net Core 在Linux系统下创建服务
linux·运维·.netcore
*长铗归来*11 天前
ASP.NET Core Web API 中控制器操作的返回类型及Swagger
后端·c#·asp.net·.netcore
IDOlaoluo11 天前
VS2017 安装 .NET Core 2.2 SDK 教程(包括 dotnet-sdk-2.2.108-win-x64.exe 安装步骤)
.netcore
csdn_aspnet19 天前
使用 Entity Framework Code First 方法创建 ASP.NET Core 5.0 Web API
.netcore·webapi
小先生81219 天前
.NET Core项目中 Serilog日志文件配置
c#·.netcore
爱吃香蕉的阿豪19 天前
.NET Core 中 System.Text.Json 与 Newtonsoft.Json 深度对比:用法、性能与场景选型
数据库·json·.netcore
csdn_aspnet19 天前
ASP.NET Core 10.0 的主要变化
.netcore
csdn_aspnet22 天前
在 C# .NETCore 中使用 MongoDB(第 1 部分):驱动程序基础知识和插入文档
mongodb·.netcore
csdn_aspnet22 天前
在 C# .NETCore 中使用 MongoDB(第 3 部分):跳过、排序、限制和投影
mongodb·c#·.netcore