Is ConfigurationManager.AppSettings available in .NET Core 2.0?

前端 未结 5 1734
孤街浪徒
孤街浪徒 2020-11-29 01:05

I\'ve got a method that reads settings from my config file like this:

var value = ConfigurationManager.AppSettings[key];

It compiles fine

相关标签:
5条回答
  • 2020-11-29 01:16

    The latest set of guidance is as follows: (from https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#environment-variables)

    Use:

    System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);

    From the docs:

    public static class EnvironmentVariablesExample
    {
        [FunctionName("GetEnvironmentVariables")]
        public static void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            log.LogInformation(GetEnvironmentVariable("AzureWebJobsStorage"));
            log.LogInformation(GetEnvironmentVariable("WEBSITE_SITE_NAME"));
        }
    
        public static string GetEnvironmentVariable(string name)
        {
            return name + ": " +
                System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
        }
    }
    

    App settings can be read from environment variables both when developing locally and when running in Azure. When developing locally, app settings come from the Values collection in the local.settings.json file. In both environments, local and Azure, GetEnvironmentVariable("<app setting name>") retrieves the value of the named app setting. For instance, when you're running locally, "My Site Name" would be returned if your local.settings.json file contains { "Values": { "WEBSITE_SITE_NAME": "My Site Name" } }.

    The System.Configuration.ConfigurationManager.AppSettings property is an alternative API for getting app setting values, but we recommend that you use GetEnvironmentVariable as shown here.

    0 讨论(0)
  • 2020-11-29 01:23

    Once you have the packages setup, you'll need to create either an app.config or web.config and add something like the following:

    <configuration>
      <appSettings>
        <add key="key" value="value"/>
      </appSettings>
    </configuration>
    
    0 讨论(0)
  • 2020-11-29 01:25

    You can use Configuration to resolve this.

    Ex (Startup.cs):

    You can pass by DI to the controllers after this implementation.

    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    
            Configuration = builder.Build();
    
        }
    
        public IConfiguration Configuration { get; }
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
    
            var microserviceName = Configuration["microserviceName"];
    
           services.AddSingleton(Configuration);
    
           ...
        }
    
    0 讨论(0)
  • 2020-11-29 01:32

    Yes, ConfigurationManager.AppSettings is available in .NET Core 2.0 after referencing NuGet package System.Configuration.ConfigurationManager.

    Credits goes to @JeroenMostert for giving me the solution.

    0 讨论(0)
  • 2020-11-29 01:41

    I installed System.Configuration.ConfigurationManager from Nuget into my .net core 2.2 application.

    I then reference using System.Configuration;

    Next, I changed

    WebConfigurationManager.AppSettings
    
    to ..
    
    ConfigurationManager.AppSettings
    

    So far I believe this is correct. 4.5.0 is typical with .net core 2.2

    I have not had any issues with this.

    0 讨论(0)
提交回复
热议问题