无法获得.NET Core中自定义应用设置的价值

时间:2018-09-25 00:53:24

标签: c# .net-core

我正在添加自定义应用程序设置部分,但在检索新添加的值时遇到了麻烦。这是一个.NET Core控制台项目。这是我的配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="customAppSettings" type="MetricCollection.MetricCustomAppSettingSection, MetricCollection"/>
    </configSections>

    <appSettings>
        <add key="metricTrackers" value=".\Trackers"/>
    </appSettings>

    <customAppSettings
        productionServers="comma,seperated,list"
        qaServers="other,comma,seperated,list">

        <test
          default="testvalue" />
    </customAppSettings>
</configuration>

我可以获取productionServers和qaServers值,但是无法获取测试条目的值。我正计划将此实现用于多个项目,因此我有一个继承自的基类。我也尝试过将所有内容放在子类中,以查看是否是问题所在,并且也不起作用。这是子类:

public class MetricCustomAppSettingSection : BaseCustomAppSettingSection
{
    [ConfigurationProperty("test", IsRequired = true)]
    public CustomAppSettingElement Test
    {
        get { return (CustomAppSettingElement)base["test"]; }
    }
}

public class MetricConfiguration : BaseConfiguration
{
    private static MetricCustomAppSettingSection _customSection;

    static MetricConfiguration()
    {
        _customSection = ConfigurationManager.GetSection("customAppSettings") as MetricCustomAppSettingSection;
    }

    // This doesn't work...
    public static string Test
    {
        get { return GetAppSetting("Test", _customSection); }
    }
}

这是基类:

public class CustomAppSettingElement : ConfigurationElement
{
    #region Properties

    /// <summary>
    /// Default value which is required to be in each element
    /// </summary>
    [ConfigurationProperty("default", IsRequired = true)]
    public string Default { get; set; }

    /// <summary>
    /// Development value which is optional for each element
    /// </summary>
    [ConfigurationProperty("development", IsRequired = false)]
    public string Development { get; set; }

    /// <summary>
    /// QA value which is optional for each element
    /// </summary>
    [ConfigurationProperty("qa", IsRequired = false)]
    public string QA { get; set; }

    /// <summary>
    /// Production value which is optional for each element
    /// </summary>
    [ConfigurationProperty("production", IsRequired = false)]
    public string Production { get; set; }

    #endregion
}

public class BaseCustomAppSettingSection : ConfigurationSection
{
    /// <summary>
    /// List of servers that are running the Production Zarga API
    /// </summary>
    [ConfigurationProperty("productionServers", IsRequired = true)]
    public string ProductionServers
    {
        get { return (string)base["productionServers"]; }
    }

    /// <summary>
    /// List of servers that are running the QA Zarga API
    /// </summary>
    [ConfigurationProperty("qaServers", IsRequired = true)]
    public string QaServers
    {
        get { return (string)base["qaServers"]; }
    }
}

public class BaseConfiguration
{
    #region Fields

    protected static bool _isQa = false;
    protected static bool _isDevelopment = false;
    protected static bool _isProduction = false;

    #endregion Fields

    static BaseConfiguration()
    {
        string host = IPGlobalProperties.GetIPGlobalProperties().HostName;
        BaseCustomAppSettingSection baseCustomSection = ConfigurationManager.GetSection("customAppSettings") as BaseCustomAppSettingSection;

        if (baseCustomSection.QaServers.IndexOf(host, StringComparison.OrdinalIgnoreCase) >= 0)
        {
            _isQa = true;
        }
        else if (baseCustomSection.ProductionServers.IndexOf(host, StringComparison.OrdinalIgnoreCase) >= 0)
        {
            _isProduction = true;
        }
        else
        {
            _isDevelopment = true;
        }
    }

    protected static string GetCustomAppSetting(string defaultValue, string qaValue, string devValue, string prodValue)
    {
        if (_isQa)
        {
            return qaValue ?? defaultValue;
        }
        else if (_isDevelopment)
        {
            return devValue ?? defaultValue;
        }
        else
        {
            return prodValue ?? defaultValue;
        }
    }

    /// <summary>
    /// Gets the setting designated by the key.
    /// </summary>
    /// <param name="key">The key whose setting you want to get.</param>
    /// <returns></returns>
    protected static string GetAppSetting(string key, BaseCustomAppSettingSection customSection)
    {
        CustomAppSettingElement customSetting = (CustomAppSettingElement)customSection.GetType().GetProperty(key).GetValue(customSection);

        return GetCustomAppSetting(customSetting.Default, customSetting.QA, customSetting.Development, customSetting.Production);
    }
}

感谢您的帮助。

0 个答案:

没有答案