CloudConfigurationManager.GetSetting returning null

与世无争的帅哥 提交于 2019-12-17 23:39:14

问题


Following instructions here I have:

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

But connectionString is null, here is my 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>

回答1:


As per documentation in 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.




回答2:


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

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



回答3:


Well this works, even if the comment doesn't fit, because I do have a ref to 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:

using System.Configuration;

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



回答4:


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.

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.




回答5:


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.




回答6:


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

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

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



回答7:


Based on my understanding, I'd like to point out that CloudConfigurationManager.GetSetting will look into web.config if you're running out of a cloud service. It will look into cscfg if you're inside a cloud service.

Please refer this link.




回答8:


Following this tutorial:

You can get configuration settings like this:

RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString")



回答9:


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

  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.



回答10:


I had the same problem (two times). Even after restarting Visual Studio and after restarting the Azure emulator the CloudConfigurationManager.GetSetting("SettingName") returns null.

I was sure that it has worked before and I had the latest SDK.

So the solutions was restarting my PC and after that CloudConfigurationManager.GetSetting("SettingName") returns the right value.




回答11:


I got the same issue this am after revisiting my Azure solution (Web + Worker role) to update it for Azure 2.5. Reviewing the help for CloudConfigurationManager.GetSetting, if its running under a cloud platform (Azure) it reads from the ServiceConfiguration.csfg, if running as a .net web app, reads from app or web.config.

So my fix was to simply change the start up project back to the Azure cloud project, not the Web project.
I was getting null because it was hosted in the wrong platform and reading from the .config files with no settings. (Doh!)




回答12:


It is an old thread but I wanted to share my solution if issue is not resolved by above mentioned methods then make sure that Azure Storage Emulator is running when you run the application; at least for me this happened. For me I had to create a class to handle emulator issue as mentioned here...

http://blog.simontimms.com/2013/08/28/configuration-settings-in-an-azure-worker-role/

class ConfigurationProvider
{
    private static string GetStorageConnectionString(string name)
    {
        try
        {
            return RoleEnvironment.GetConfigurationSettingValue(name);
        }
        catch (SEHException)
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings[name].ConnectionString;
        }
    }

    public static string StorageConnectionString()
    {
        return GetStorageConnectionString("StorageConnectionString");
    }

    public static string DefaultConnection()
    {
        return GetStorageConnectionString("DefaultConnection");
    }
}



回答13:


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

  • 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).




回答14:


Same here after upgrading Azure SDK from 2.2 to 2.3.:

Right-Click the Azure project select Properties. In the Application tab click "Upgrade..." (Thanks to rattrick's answer).

Then there was one more error to resolve: Trying to run the Azure Project in the Compute Emulator threw an exception: System.Configuration.ConfigurationErrorsException was unhandled Message: An unhandled exception of type 'System.Configuration.ConfigurationErrorsException' occurred in Microsoft.WindowsAzure.ServiceRuntime.dll Additional information: konnte nicht erstellt werden.

In the "Error List" Window of VS2013 there was the following Warning:

Found conflicts between different versions of the same dependent assembly. In Visual Studio, double-click this warning (or select it and press Enter) to fix the conflicts; otherwise, add the following binding redirects to the "runtime" node in the application configuration file: C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets 1635

I let VS resolve this warning and everything worked fine.




回答15:


This worked for me...

using System.Configuration;

...

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



回答16:


I had the same problem. None of the advices worked for me, but the "issue" was straightforward. One simple has to understand how this class works.

It does not go into your app.config / web.config or wherever you store your application settings. CloudConfigurationManager works with ServiceConfiguration.*.cscfg and ServiceConfiguration.csdef. The .csdef must contain a definition of the setting, but not its value under the ConfigurationSettings section. The settings themselves sit in .cscfg files (under the same section but including the value; I suppose the reason for the double definition is to make sure both the cloud and the local configurations have the same settings).

You can set these either by right-clicking your role in Visual Studio and selecting Properties -> Settings (in case of StorageConnectionString, simply pick "Your subscription", if your storage account is connected to the cloud service), or by editing them manually. If you mess up the settings, you'll get an exclamation mark icon.

Simple as that.




回答17:


Was getting a null value with when passing a literal string as well after installing Azure SDK 2.6 (was working before).

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

Replaced the literal string and it worked fine.

   string connectionStr = "AzureStorage";

   var connectionstring = ConfigurationManager.ConnectionStrings[connectionStr].ConnectionString;

   CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionstring);


来源:https://stackoverflow.com/questions/15384186/cloudconfigurationmanager-getsetting-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!