如何构建一个网站,wordpress视频缩略图不显示,wordpress 3.9 友情链接,建设网站大约多少钱目录
配置系统
默认添加的配置提供者
加载命令行中的配置。
运行环境
读取方法
User Secrets
注意事项
Zack.AnyDBConfigProvider
案例 配置系统
默认添加的配置提供者
加载现有的IConfiguration。加载项目根目录下的appsettings.json。加载项目根目录下的appsettin…目录
配置系统
默认添加的配置提供者
加载命令行中的配置。
运行环境
读取方法
User Secrets
注意事项
Zack.AnyDBConfigProvider
案例 配置系统
默认添加的配置提供者
加载现有的IConfiguration。加载项目根目录下的appsettings.json。加载项目根目录下的appsettings.{Environment}.json。当程序运行在开发环境下程序会加载“用户机密”配置。加载环境变量中的配置。 加载命令行中的配置。
运行环境
ASP.NET Core 会从环境变量中读取名字为ASPNETCORE_ENVIRONMENT的值。推荐值Development开发环境、Staging测试环境、Production生产环境。
读取方法
app.Environment.EnvironmentName、app.Environment.IsDevelopment()……
[HttpGet]
public string Test()
{return Environment.GetEnvironmentVariable(haha);
}
Program
app.Environment.IsDevelopment()Controller
private readonly IWebHostEnvironment webHostEnvironment;[HttpGet]
public string Test()
{return webHostEnvironment.EnvironmentName;
}
User Secrets
把不方便放到appsettings.json中的机密信息放到一个不在项目中的json文件中。在ASP.NET Core项目上单击鼠标右键选择【管理用户机密】。C:\Users\用户名\AppData\Roaming\Microsoft\UserSecrets\473b31c4-af83-4603-8d28-438df885bdef
注意事项
供开发人员使用的不适合在生产环境中使用。仍然是明文存储。不想别人看到怎么办Azure Key Vault、Zack.AnyDBConfigProvider等。无法完全避免。加强安全防控更重要。如果因为重装、新员工等原因导致secrets.json重建就要重新配置麻烦。如果影响大的话还是用集中式配置服务器。
Zack.AnyDBConfigProvider
https://github.com/yangzhongke/Zack.AnyDBConfigProviderhttps://github.com/yangzhongke/Zack.AnyDBConfigProviderhttps://github.com/yangzhongke/Zack.AnyDBConfigProviderhttps://github.com/yangzhongke/Zack.AnyDBConfigProvider
案例
系统的主要配置Redis、Smtp放到配置专用的数据库中。Zack.AnyDBConfigProvider连接配置数据库的连接字符串配置在“用户机密”中。Data Source.;Initial Catalogdemo1;Integrated SecuritySSPI;把Smtp的配置显示到界面上。程序启动的时候就连接Redis并且把Redis连接对象注册到依赖注入系统中。
secrets.json:
{connStr: Data Source.;Initial Catalogdemo1;Integrated SecuritySSPI;TrustServerCertificatetrue;
}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.ConfigureSmtpSettings(builder.Configuration.GetSection(Smtp));
//添加Redis配置源
builder.Services.AddSingletonIConnectionMultiplexer(sp
{//在Program.cs中读取配置的一种方法string constr builder.Configuration.GetSection(Redis).Value;return ConnectionMultiplexer.Connect(constr);
});Controller:
private readonly IOptionsSnapshotSmtpSettings optSmtp;
private readonly IConnectionMultiplexer connectionMultiplexer;public Test(IOptionsSnapshotSmtpSettings optSmtp, IConnectionMultiplexer connectionMultiplexer)
{this.optSmtp optSmtp;this.connectionMultiplexer connectionMultiplexer;
}[HttpGet]
public string Demo1()
{var ping connectionMultiplexer.GetDatabase(0).Ping();return optSmtp.Value.ToString() : ping;
}