C# CloudConfigurationManager.GetSetting 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15384186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:36:16  来源:igfitidea点击:

CloudConfigurationManager.GetSetting returning null

c#azureapp-configazure-storage

提问by weston

Following instructions hereI have:

按照这里的说明我有:

var connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");

But connectionStringis null, here is my app.config:

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>

采纳答案by weston

Well this works, even if the comment doesn't fit, because I do have a ref to CloudConfigManager:

好吧,即使评论不合适,这也行得通,因为我确实有对 CloudConfigManager 的引用:

If you are creating an application with no reference to Microsoft.WindowsAzure.CloudConfigurationManager, and your connection string is located in the web.config or app.config as show above, then you can use ConfigurationManager to retrieve the connection string. You will need to add a reference to System.Configuration.dll to your project and add another namespace declaration for it:

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

using System.Configuration;

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

回答by SocialMitch

Make sure all your references are in synch. There's the 2012-06 library and 2012-10 Set them to Copy Local = true and verify SDK version. I dealt with the exact same thing, drove me nuts.

确保所有参考文献都是同步的。有 2012-06 库和 2012-10 将它们设置为 Copy Local = true 并验证 SDK 版本。我处理了完全相同的事情,让我发疯。

回答by Scott Scowden

I had the same problem. I had updated the project to use Azure SDK 2.0. I updated NuGet packages for my web and worker roles, but the Azure project in Visual Studio was still on the old version.

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

To fix this, Right-Click on your Azure project and select Properties. Under the Application tab, you'll see a button to Update your Azure SDK.

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

回答by Nikolai Joukov

As per documentation in MSDN http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.cloudconfigurationmanager.aspx

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

Only configuration settings within the appSettings tag can be read by CloudConfigurationManager. If your configuration settings are within a different tag, calling GetSetting will return Null.

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

回答by spadelives

This worked for me...

这对我有用...

using System.Configuration;

...

...

var connectionString = ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString;

回答by Matt Frear

This happened to me when I upgraded the Azure SDK to version 2.2.

当我将 Azure SDK 升级到 2.2 版时,这发生在我身上。

To fix it I changed the packages.config to use a newer version of the Azure ConfigurationManager.

为了修复它,我更改了 packages.config 以使用更新版本的 Azure ConfigurationManager。

<package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.1.0" targetFramework="net45" />

回答by BerndK

I had quite similar problems. I updated from Azure SDK 2.0 to 2.2 - during this process I used the NuGet Manager to update the Microsoft.WindowsAzure.Storage to the latest. The PackageManager automatically took Microsoft.WindowsAzure.Configuration to 1.8.0.0. I was not able to get this running (it was for .Net 2.0!?). After I manually set all References to

我有非常相似的问题。我从 Azure SDK 2.0 更新到 2.2 - 在此过程中,我使用 NuGet 管理器将 Microsoft.WindowsAzure.Storage 更新到最新版本。PackageManager 自动将 Microsoft.WindowsAzure.Configuration 升级为 1.8.0.0。我无法运行它(它用于 .Net 2.0!?)。在我手动将所有引用设置为

  • Microsoft.WindowsAzure.Storage 2.1.0.0
  • Microsoft.WindowsAzure.Configuration 2.0.0.0
  • Microsoft.WindowsAzure.Storage 2.1.0.0
  • Microsoft.WindowsAzure.Configuration 2.0.0.0

everything worked.

一切正常。

I think this is because of the way CloudConfigurationManager.GetSetting loads the assembly and calls the funktions (via reflection).

我认为这是因为 CloudConfigurationManager.GetSetting 加载程序集并调用函数的方式(通过反射)。

回答by user888734

Following this tutorial:

按照本教程:

You can get configuration settings like this:

您可以获得这样的配置设置:

RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString")

回答by acarlon

I got this after upgrading Azure SDK from 2.0 to 2.2. I was able to fix by:

我在将 Azure SDK 从 2.0 升级到 2.2 后得到了这个。我能够通过以下方式修复:

  1. Right-Clicking Azure project and selecting Properties. Update Azure SDK as per the Application tab. (Thanks to rattrick's answer).
  2. Right click to Manage NuGet Packages. On the left click Updates and update WindowsAzure.ConfigurationManager.
  1. 右键单击 Azure 项目并选择属性。根据应用程序选项卡更新 Azure SDK。(感谢拉特里克的回答)。
  2. 右键单击以管理 NuGet 包。在左侧单击更新并更新 WindowsAzure.ConfigurationManager。

回答by dascalos

Had the same problem. Instead of using a connection string, use the configuration->appSettings->add key like this...

有同样的问题。不要使用连接字符串,而是使用 configuration->appSettings->add 这样的键...

<configuration>
    <appSettings>
        <add key="StorageConnectionString" value="[ConnectionStringHere]" />
    </appSettings>
</configuration>