CloudConfigurationManager不拿起从app.config中的applicationSettings [英] CloudConfigurationManager does not pick up ApplicationSettings from app.config

查看:206
本文介绍了CloudConfigurationManager不拿起从app.config中的applicationSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些Azure的辅助类的库。这些助手类中我获得设置,如Azure的帐户名和密钥。当在Azure上运行这些设置是从云配置文件(cscfg)接走。这一切工作正常。

为了天青以外的这些类(特别是RoleEnvironment)我的单元测试项目中创建相同的变量名的设置,单元测试。这些实际上得到保存一个app.config文件中,并通过设置部分这下我的测试项目的属性部分的生活是编辑。而不是创建自己的自的web.config / app.config中设置抽象的云配置设置的方法,我决定使用CloudConfigurationManager类。然而,当我跑我的单元测试都没有我的设置拾取,所以我只是得到空值。但是,如果我改变我的app.config文件下的appSettings'格式使用的设置,然后我得到有效的值。这种方法的缺点是,我可以使用Visual Studio中的设置编辑页面不再修改我的设置。

所以我的问题是我做错了什么,或这是云配置管理器,因此它只能拿起手动添加的appSettings但不是限制的applicationSettings使用编辑器添加的?

 <&的appSettings GT;
    <添加键=富值=MySettingValue/>
< /的appSettings>

以上的作品,而下面的不会:

 <&的applicationSettings GT;
    < ComponentsTest.Properties.Settings>
      <设定名=富serializeAs =字符串>
        < VALUE> MySettingValue< /值>
      < /设置>
    < /ComponentsTest.Properties.Settings>
< /&的applicationSettings GT;


解决方案

该CloudConfigurationManager仅支持在的AppSettings 在web.config / app.config中的一部分,并会尝试从这里,如果读值设置从Azure的配置中缺少。该文件指出,它的不是将的阅读的web.config /的app.config如果属性RoleEnvironment.IsAvailable是的真正的(在Azure中运行),但是这是<强>不正确,因为我们可以在下面(不检查IsAvailable)源$ C ​​$ C见

您可以看看在<一个href=\"https://github.com/WindowsAzure/azure-sdk-for-net/blob/master/microsoft-azure-api/Configuration/Microsoft.WindowsAzure.Configuration/AzureApplicationSettings.cs\">source看看会发生什么:

  ///&LT;总结&gt;
    ///获取与给定名称的设置。
    ///&LT; /总结&gt;
    ///&LT; PARAM NAME =名方式&gt;设置名称&LT; /参数&GT;
    ///&LT;退货和GT;设置值,或者如果这样的设置不存在空&LT; /回报&GT;
    内部字符串GetSetting(字符串名称)
    {
        Debug.Assert的(string.IsNullOrEmpty(名)!);        字符串值= NULL;        值=的GetValue(ServiceRuntime,名称,GetServiceRuntimeSetting);
        如果(价值== NULL)
        {
            值=的GetValue(ConfigurationManager中,姓名,N =&GT; ConfigurationManager.AppSettings [N]);
        }        返回值;
    }

正如你所看到的,只有一个呼叫到正常的 ConfigurationManager中类,只需访问的AppSettings

I have a library containing some Azure helper classes. Inside these helper classes I obtain settings such as the Azure account name and key. When running in Azure these settings are picked up from the cloud configuration file (cscfg). This all works fine.

In order to unit test these classes outside of Azure (specifically the RoleEnvironment) I created settings of the same variable names within the unit test project. These actually get saved within an app.config file and are edited via the settings section which lives under the properties section of my test project. Rather than create my own method of abstracting the cloud configuration settings from web.config/app.config settings I decided to use the CloudConfigurationManager class. However, when I run my unit tests none of my settings are picked up so I simply get nulls. If however, I change my app.config file to use settings in the 'appSettings' format below, then I do get valid values. The downside of this is I can no longer edit my settings using the settings editor page within visual studio.

So my question is am I doing something wrong or is this a limitation of the cloud configuration manager, whereby it can only pick up manually added appSettings but not applicationSettings added using the editor?

<appSettings>
    <add key="Foo" value="MySettingValue"/>
</appSettings>

the above works, whereas the below does not:

<applicationSettings>
    <ComponentsTest.Properties.Settings>
      <setting name="Foo" serializeAs="String">
        <value>MySettingValue</value>
      </setting>
    </ComponentsTest.Properties.Settings>  
</applicationSettings>

解决方案

The CloudConfigurationManager only supports the AppSettings part of the web.config/app.config and will try to read values from here if the setting is missing from the Azure configuration. The documentation states that it won't read the web.config/app.config if the property RoleEnvironment.IsAvailable is true (running in Azure), but that is incorrect as we can see in the source code below (no check for IsAvailable).

You can take a look at the source to see what happens:

    /// <summary>
    /// Gets a setting with the given name.
    /// </summary>
    /// <param name="name">Setting name.</param>
    /// <returns>Setting value or null if such setting does not exist.</returns>
    internal string GetSetting(string name)
    {
        Debug.Assert(!string.IsNullOrEmpty(name));

        string value = null;

        value = GetValue("ServiceRuntime", name, GetServiceRuntimeSetting);
        if (value == null)
        {
            value = GetValue("ConfigurationManager", name, n => ConfigurationManager.AppSettings[n]);
        }

        return value;
    }

As you can see, there is only one call to the normal ConfigurationManager class that simply accesses the AppSettings.

这篇关于CloudConfigurationManager不拿起从app.config中的applicationSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆