ASP.NET Core—access Configuration from static class

后端 未结 13 1980
我在风中等你
我在风中等你 2021-02-01 00:27

I want a simple static class that accesses the Configuration object. All the config info is already read in from the appsettings.json file in the Startup class. I just need an e

相关标签:
13条回答
  • 2021-02-01 00:33

    I agree with mcbowes, it's in the docs, but the first example looks more like what you need...want:

    public class Program
    {
        public static IConfigurationRoot Configuration { get; set; }
        public static void Main(string[] args = null)
        {
            var builder = new ConfigurationBuilder()
                 .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");
    
            Configuration = builder.Build();
    
            Console.WriteLine($"option1 = {Configuration["option1"]}");
    
            // Edit:
            IServiceCollection services = new ServiceCollection();
            services.AddOptions();
            services.Configure<HelloWorldOptions>(_configuration.GetSection("HelloWorld"));
            // And so on...
        }
    }
    
    0 讨论(0)
  • 2021-02-01 00:33

    The IConfiguration is Injectable anywhere within the Project. But in the case of static class, the option I am using and maybe only approach... var Configuration = new ConfigurationBuilder() .AddUserSecrets<Startup>() .Build(); And, you can add required section, such in this code block above, I added 'UserSecrets'.

    0 讨论(0)
  • 2021-02-01 00:35

    If you are using environment variables as your configuration, you can access the environment variable directly rather than via the configuration object.

    using System;
    
    namespace My.Example
    {
        public static class GetPaths
        {
            private static readonly string MyPATH = 
                Environment.GetEnvironmentVariable("PATH");
    
            private static readonly string MySpecialPath =
                Environment.GetEnvironmentVariable("PREFIX_SpecialPath");
            ...
        }
    }
    
    0 讨论(0)
  • 2021-02-01 00:36

    Personally I like the method used in this link

    Essentially it just adding a static field to your options class.

     public class WeblogConfiguration
     {
        public static WeblogConfiguration Current;
    
        public WeblogConfiguration()
        {
            Current = this;
        }
    } 
    

    Then in any static class you can do:

    WeblogConfiguration.Current
    

    Simple and very straight forward

    0 讨论(0)
  • 2021-02-01 00:37

    I think you could use extension function, something like this

    public static string ConfigToSomeThing(this IConfiguration config, int value)
            {
                return config[value.ToString()] ?? "";
            }
    

    Then any place , just injection IConfiguration and use extension method

    _systemConfiguration.ConfigToSomeThing(123);
    
    0 讨论(0)
  • 2021-02-01 00:39

    Consider using the instructions here for ASP.NET Core Configuration.

    You can create a class to store your configuration settings and then access the values, something like this:

    _config.UserName
    

    In Startup - ConfigureServices:

    services.Configure<Config>(Configuration.GetSections("General"));
    

    Then just inject your object wherever you need as:

    IOptions<Config> config
    
    0 讨论(0)
提交回复
热议问题