这是今天下午一个同事问我的问题,如何在静态类中读取json配置文件。我当时并没有告诉他如何如何去做,办法肯定是有,但是这种编程思维确实得改改了。静态类、静态方法不是面向对象编程的最佳实践。.NET Core已经为了我们提供了一种通过Dependency Injection获取配置文件的方法。作为开发人员,我们需要遵循这种范式转换才能很好地使用.Net Core。传送门:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/options   

对于这个问题还是得解决:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
public static class AppSettingsProvider
{
    public static string DbConnectionString { get; set; }
}
public Startup(IConfiguration configuration)
{
    Configuration = configuration;

    BuildAppSettingsProvider();
}
private void BuildAppSettingsProvider() { AppSettingsProvider.ConnectionString = Configuration.GetConnectionString("DBContext"); }

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄