Is ConfigurationManager.AppSettings available in .NET Core 2.0?

前端 未结 5 1749
孤街浪徒
孤街浪徒 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("") 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.

提交回复
热议问题