CloudConfigurationManager.GetSetting返回null

wes*_*ton 22 c# app-config azure azure-storage

按照这里的说明,我有:

var connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");
Run Code Online (Sandbox Code Playgroud)

connectionStringnull,这里是我的app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="StorageConnectionString"
         connectionString="DefaultEndpointsProtocol=https;AccountName=storage;AccountKey=key" />
  </connectionStrings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.0.0" newVersion="5.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)

小智 20

根据MSDN中的文档http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.cloudconfigurationmanager.aspx

CloudConfigurationManager只能读取appSettings标记内的配置设置.如果您的配置设置在不同的标记内,则调用GetSetting将返回Null.


das*_*los 18

有同样的问题.而不是使用连接字符串,使用配置 - > appSettings->这样的添加键...

<configuration>
    <appSettings>
        <add key="StorageConnectionString" value="[ConnectionStringHere]" />
    </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

  • 这不是答案 (2认同)
  • 这是我的答案.我在Azure项目设置中有连接字符串,它突然停止为我工作,没有任何我能想到的解释(没有更新SDK或nuget包等).将appSetting添加到Web.config也适用于我.谢谢您的帮助 (2认同)

wes*_*ton 9

这很有用,即使评论不合适,因为我确实有一个参考CloudConfigManager:

如果您创建的应用程序没有引用Microsoft.WindowsAzure.CloudConfigurationManager,并且您的连接字符串位于web.config或app.config中,如上所示,则可以使用ConfigurationManager检索连接字符串.您需要向项目添加对System.Configuration.dll的引用,并为其添加另一个名称空间声明:

using System.Configuration;

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
Run Code Online (Sandbox Code Playgroud)

  • 我不明白为什么这个答案标记正确.发布的问题引用了CloudConfigurationManager,但此解决方案使用ConfigurationManager.它们不是同一件事.在我看来,尼古拉·朱科夫对问题的回答是正确的. (10认同)

Sco*_*den 9

我有同样的问题.我已将项目更新为使用Azure SDK 2.0.我为我的Web和辅助角色更新了NuGet包,但Visual Studio中的Azure项目仍然是旧版本.

要解决此问题,请右键单击Azure项目,然后选择"属性".在"应用程序"选项卡下,您将看到一个用于更新Azure SDK的按钮.