我有一个方法,读取设置从我的配置文件像这样:

var value = ConfigurationManager.AppSettings[key];

它在仅针对. net标准2.0时编译良好。

现在我需要多个目标,所以我更新了我的项目文件:

<TargetFrameworks>netcoreapp2.0;net461;netstandard2.0</TargetFrameworks>

但是现在,netcoreapp2.0的编译失败,并出现以下错误消息:

名称“ConfigurationManager”在当前上下文中不存在(netcoreapp2.0)

另外,我创建了一个新的。net Core 2.0控制台应用程序(这次只针对。net Core 2.0),但同样地,在命名空间System.Configuration下似乎没有ConfigurationManager。

我很困惑,因为它在。net标准2.0下可用,所以我希望它在。net核心2.0中可用,因为。net核心2.0是。net标准2.0兼容的。

我错过了什么?


当前回答

我将System.Configuration.ConfigurationManager从Nuget安装到。net core 2.2应用程序中。

然后我引用使用System.Configuration;

接下来,我改变了

WebConfigurationManager.AppSettings

to ..

ConfigurationManager.AppSettings

到目前为止,我认为这是正确的。4.5.0是典型的。net core 2.2

我在这方面没有任何问题。

其他回答

我将System.Configuration.ConfigurationManager从Nuget安装到。net core 2.2应用程序中。

然后我引用使用System.Configuration;

接下来,我改变了

WebConfigurationManager.AppSettings

to ..

ConfigurationManager.AppSettings

到目前为止,我认为这是正确的。4.5.0是典型的。net core 2.2

我在这方面没有任何问题。

最新的指南如下:(来自https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#environment-variables)

Use:

System.Environment。GetEnvironmentVariable(名称、EnvironmentVariableTarget.Process);

从文档中可以看出:

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.

是的,ConfigurationManager。在引用NuGet包System.Configuration.ConfigurationManager后,可以在. net Core 2.0中使用AppSettings。

感谢@JeroenMostert给我的解决方案。

我使用下面的代码示例。而且这是非常方便的方式。

using Microsoft.Extensions.Configuration;
using System.IO;

namespace DemoWeppApp
{
    public static class StaticConfigurationManager
    {
        public static IConfiguration AppSetting { get; }
        static StaticConfigurationManager()
        {
            AppSetting = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json")
                    .Build();
        }
    }
}

我可以在任何静态类中使用

StaticConfigurationManager.AppSetting["conf_name"];

一旦你有包设置,你需要创建一个app.config或web。配置并添加如下内容:

<configuration>
  <appSettings>
    <add key="key" value="value"/>
  </appSettings>
</configuration>