首页 文章

如何在.NET Core中将appsetting.json部分加载到Dictionary中?

提问于
浏览
13

我很熟悉将appsettings.json部分加载到.NET Core startup.cs中的强类型对象中 . 例如:

public class CustomSection 
{
   public int A {get;set;}
   public int B {get;set;}
}

//In Startup.cs
services.Configure<CustomSection>(Configuration.GetSection("CustomSection"));

//Inject an IOptions instance
public HomeController(IOptions<CustomSection> options) 
{
    var settings = options.Value;
}

我有一个appsettings.json部分,其键/值对随着时间的推移会在数量和名称上有所不同 . 因此,对类中的属性名称进行硬编码是不切实际的,因为新的键/值对需要在类中进行代码更改 . 一些键/值对的小样本:

"MobileConfigInfo": {
    "appointment-confirmed": "We've booked your appointment. See you soon!",
    "appointments-book": "New Appointment",
    "appointments-null": "We could not locate any upcoming appointments for you.",
    "availability-null": "Sorry, there are no available times on this date. Please try another."
}

有没有办法将此数据加载到MobileConfigInfo Dictionary对象中,然后使用IOptions模式将MobileConfigInfo注入控制器?

8 回答

  • 0

    您可以在 startup.cs 类中使用 Configuration.Bind(settings);

    你的设置类就像

    public class AppSettings
    {
        public Dictionary<string, string> MobileConfigInfo
        {
            get;
            set;
        }
    }
    

    希望能帮助到你!

  • 12

    使用这种结构格式:

    "MobileConfigInfo": {
        "Values": {
           "appointment-confirmed": "We've booked your appointment. See you soon!",
           "appointments-book": "New Appointment",
           "appointments-null": "We could not locate any upcoming appointments for you.",
           "availability-null": "Sorry, there are no available times on this date. Please try another."
     }
    }
    

    使您的设置类看起来像这样:

    public class CustomSection 
    {
       public Dictionary<string, string> Values {get;set;}
    }
    

    然后这样做

    services.Configure<CustomSection>((settings) =>
    {
         Configuration.GetSection("MobileConfigInfo").Bind(settings);
    });
    
  • 0

    对于想要将其转换为 Dictionary 的其他人,

    appsettings.json中的示例部分

    "MailSettings": {
        "Server": "http://mail.mydomain.com"        
        "Port": "25",
        "From": "info@mydomain.com"
     }
    

    以下代码应放在Startup文件> ConfigureServices方法中:

    public static Dictionary<string, object> MailSettings { get; private set; }
    
    public void ConfigureServices(IServiceCollection services)
    {
        //ConfigureServices code......
    
        MailSettings = 
            Configuration.GetSection("MailSettings").GetChildren()
            .Select(item => new KeyValuePair<string, string>(item.Key, item.Value))
            .ToDictionary(x => x.Key, x => x.Value);
    }
    

    现在您可以从以下任何地方访问字典:

    string mailServer = Startup.MailSettings["Server"];
    

    一个缺点是所有值都将作为字符串检索,如果您尝试任何其他类型,该值将为null .

  • 2

    对于简单(可能是微服务)应用程序,您只需将其作为单例 Dictionary<string, string> 添加,然后将其注入到您需要的任何位置:

    var mobileConfig = Configuration.GetSection("MobileConfigInfo")
                        .GetChildren().ToDictionary(x => x.Key, x => x.Value);
    
    services.AddSingleton(mobileConfig);
    

    用法:

    public class MyDependantClass
    {
        private readonly Dictionary<string, string> _mobileConfig;
    
        public MyDependantClass(Dictionary<string, string> mobileConfig)
        {
            _mobileConfig = mobileConfig;
        }
    
        // Use your mobile config here
    }
    
  • 0

    我相信你可以使用以下代码:

    var config =  Configuration.GetSection("MobileConfigInfo").Get<Dictionary<string, string>>();
    
  • 15

    作为ASP.Net Core 2.1中更复杂绑定的示例;根据the documention,我发现使用 ConfigurationBuilder .Get<T>() 方法更容易使用 .

    ASP.NET Core 1.1及更高版本可以使用Get,它适用于整个部分 . 获取比使用Bind更方便 .

    我在 Startup 方法中绑定了配置 .

    private Config Config { get; }
    
    public Startup(IConfiguration Configuration)
    {
        Config = Configuration.Get<Config>();
    }
    

    这绑定了 appsettings 文件:

    {
        "ConnectionStrings": {
            "Accounts": "Server=localhost;Database=Accounts;Trusted_Connection=True;",
            "test": "Server=localhost;Database=test;Trusted_Connection=True;",
            "Client": "Server=localhost;Database={DYNAMICALLY_BOUND_CONTEXT};Trusted_Connection=True;",
            "Support": "Server=localhost;Database=Support;Trusted_Connection=True;"
        },
        "Logging": {
            "IncludeScopes": false,
            "LogLevel": {
                "Default": "Debug",
                "System": "Information",
                "Microsoft": "Information"
            }
        },
        "Plugins": {
            "SMS": {
                "RouteMobile": {
                    "Scheme": "https",
                    "Host": "remote.host",
                    "Port": 84567,
                    "Path": "/bulksms",
                    "Username": "username",
                    "Password": "password",
                    "Source": "CompanyName",
                    "DeliveryReporting": true,
                    "MessageType": "Unicode"
                }
            },
            "SMTP": {
                "GenericSmtp": {
                    "Scheme": "https",
                    "Host": "mail.host",
                    "Port": 25,
                    "EnableSsl": true,
                    "Username": "smtpuser@mail.host",
                    "Password": "password",
                    "DefaultSender": "noreply@companyname.co.uk"
                }
            }
        }
    }
    

    进入这个配置结构:

    [DataContract]
    public class Config
    {
        [DataMember]
        public Dictionary<string, string> ConnectionStrings { get; set; }
        [DataMember]
        public PluginCollection Plugins { get; set; }
    }
    
    [DataContract]
    public class PluginCollection
    {
        [DataMember]
        public Dictionary<string, SmsConfiguration> Sms { get; set; }
        [DataMember]
        public Dictionary<string, EmailConfiguration> Smtp { get; set; }
    }
    
    [DataContract]
    public class SmsConfiguration
    {
        [DataMember]
        public string Scheme { get; set; }
        [DataMember]
        public string Host { get; set; }
        [DataMember]
        public int Port { get; set; }
        [DataMember]
        public string Path { get; set; }
        [DataMember]
        public string Username { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public string Source { get; set; }
        [DataMember]
        public bool DeliveryReporting { get; set; }
        [DataMember]
        public string Encoding { get; set; }
    }
    
    [DataContract]
    public class EmailConfiguration
    {
        [DataMember]
        public string Scheme { get; set; }
        [DataMember]
        public string Host { get; set; }
        [DataMember]
        public int Port { get; set; }
        [DataMember]
        public string Path { get; set; }
        [DataMember]
        public string Username { get; set; }
        [DataMember]
        public string Password { get; set; }
        [DataMember]
        public string DefaultSender { get; set; }
        [DataMember]
        public bool EnableSsl { get; set; }
    }
    
  • 1

    我用下面的方法:

    appsettings.json:

    "services": {
          "user-service": "http://user-service:5000/",
          "app-service": "http://app-service:5000/"
      }
    

    startup.cs:

    services.Configure<Dictionary<string, string>>(Configuration.GetSection("services"));
    

    用法:

    private readonly Dictionary<string, string> _services;
    public YourConstructor(IOptions<Dictionary<string, string>> servicesAccessor)
    {
        _services = servicesAccessor.Value;
    }
    
  • 10

    到目前为止,最简单的方法是将配置类定义为从要支持的Dictionary类型继承 .

    public class MobileConfigInfo:Dictionary<string, string>{
    }
    

    然后,您的启动和依赖注入支持将与任何其他配置类型完全相同 .

相关问题