网站首页 全球最实用的IT互联网站!

人工智能P2P分享Wind搜索发布信息网站地图标签大全

当前位置:诺佳网 > 软件工程 > 后端开发 > .Net >

ASP.NET Core使用选项方式读取配置

时间:2025-03-08 12:36

人气:

作者:admin

标签:

导读:第一步 :新建ASP.NET Core webapi项目 第二步:安装Microsoft.Extensions.Options和Microsoft.Extensions.Configuration.Binder 第三步:修改appsettings.json,内容如下: { quot;Logging...
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "DB": {
    "DbType": "SQLServer",
    "Connection": "Data Source = .Catalog=DemoDB; Integrated Security = true"
  },
  "Smtp": {
    "Server": "smtp.youzack.com",
    "UserName": "zack",
    "Password": "hello888",
  },
    "AllowedHosts": "*"
  }
public class DbSettings
{
    public string? DbType { get; set; }
    public string? ConnectionString { get; set; }

}
 public class SmtpSettings
 {
     public string? Server { get; set; }
     public string? UserName { get; set; }
     public string? Password { get; set; }
 }

由于使用选项方式读取配置的时候,需要和依赖注入一起使用,因此我们需要创建一个类用于获取注入的选项值。声明接收选项注入的对象的类型不能直接使用DbSettings、SmtpSettings,而要使用IOptions、IOptionsMonitor、IOptionsSnapshot等泛型接口类型,因为它们可以帮我们处理容器生命周期、配置刷新等。它们的区别在于,IOptions在配置改变后,我们不能读到新的值,必须重启程序才可以读到新的值;IOptionsMonitor在配置改变后,我们能读到新的值;IOptionsSnapshot也是在配置改变后,我们能读到新的值,和IOptionsMonitor不同的是,在同一个范围内IOptionsMonitor会保持一致性。

 public class Demo
 {
     private readonly IOptionsSnapshot<DbSettings> optDbsettings;
     private readonly IOptionsSnapshot<SmtpSettings> optSmtpSettings;

     public Demo(IOptionsSnapshot<DbSettings> optDbsettings, IOptionsSnapshot<SmtpSettings> optSmtpSettings)
     {
         this.optDbsettings = optDbsettings;
         this.optSmtpSettings = optSmtpSettings;
     }


     public void Test()
     {
         var db = optDbsettings.Value;
         Console.WriteLine($"数据库:{db.DbType},{db.ConnectionString}");
         var smtp = optSmtpSettings.Value;
         Console.WriteLine($"smtp:{smtp.Server},{smtp.UserName},{smtp.Password}");
     }

 }
 var configurationBuilder = new ConfigurationBuilder();
 configurationBuilder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
 IConfigurationRoot config = configurationBuilder.Build();
 ServiceCollection services = new ServiceCollection();
 services.AddOptions().Configure<DbSettings>(e => config.GetSection("DB").Bind(e))
     .Configure<SmtpSettings>(e => config.GetSection("Smtp").Bind(e));
 services.AddTransient<Demo>();
 using (var sp = services.BuildServiceProvider())
 {
     while (true)
     {
         using (var scope = sp.CreateScope())
         {
             var spScope = scope.ServiceProvider;
             var demo = spScope.GetRequiredService<Demo>();
             demo.Test();
         }
         Console.WriteLine("可以修改配置了");
         Console.ReadKey();
     }
 }

运行exe
![[assets/ASP.NET Core使用选项方式读取配置/file-20250308123308971.png]]

修改配置文件保存
![[assets/ASP.NET Core使用选项方式读取配置/file-20250308123433037.png]]

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信