Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudConfigurationManager.GetSetting returning null

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>
like image 718
weston Avatar asked Mar 13 '13 11:03

weston


4 Answers

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> 
like image 193
dascalos Avatar answered Sep 28 '22 11:09

dascalos


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.

like image 42
Nikolai Joukov Avatar answered Sep 28 '22 11:09

Nikolai Joukov


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);
like image 40
weston Avatar answered Sep 28 '22 11:09

weston


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.

like image 29
Scott Scowden Avatar answered Sep 28 '22 10:09

Scott Scowden