Question

I have an Azure Cloud solution with a WebRole project.

Windows Azure Tools version: 2.2

packages.config in the WebRole project contains this:

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

I've "Nuget Enable Package Restore" and therefore assume the right packages are linked and referenced during build.

The following line works fine on my dev-box Azure Compute emulator, however, when I deploy (check in to TFS online, which builds the solution and kicks off deployment to Azure), this line returns null:

CloudConfigurationManager.GetSetting("StorageConnectionString")

I've read a couple of other posts on this, but haven't so far been able to resolve the issue. Is there a package/SDK incompatibility somewhere that I'm not aware of?

Was it helpful?

Solution

The problem turned out to be with how I had configured my Azure project, which I will explain below. I also asked the question in MSDN forums and got some help there.

TLDR; In Visual Studio, my project was set up as an Azure Cloud Service with a WebRole. However, I was deploying the project to an "Azure Website" in production. There are important differences between an Azure Cloud Service with a WebRole and an Azure Website.

Here's some more details: Azure can host a website/webservice as part of two different project types and service configurations. When you go to your Azure portal, and you create a new service, you can either create:

  1. Azure Cloud Service
  2. Azure Website

An Azure Cloud Service may or may not contain a WebRole. If it does, then it will be accessible through an endpoint that looks like this http://servicename.cloudapp.net. An Azure Website, on the other hand, is hosted on an endpoint like this http://websitename.azurewebsites.net. The differences are far beyond just the domain names.

WebRole vs. Website

A WebRole (which you can create as part of an "Cloud Service" solution in Visual Studio) is connected to the rest of the Cloud Service that you have created and therefore can reference and use resources that belong to the Cloud Service, such as the settings and configurations that you store within the .cscfg files. These settings are correctly resolved and referenced by the CloudConfigurationManager library.

A Website, however, is a standalone project, much like a web application that you deploy to your IIS express. A website essentially maps to a "Web Application" solution in Visual Studio. The CloudConfigurationManager library won't know what to do with this type of project even if you referenced it in your web application.

Solution for Website

Anyway, I recreated my project as a "Web Application" solution this time (as opposed to "Cloud Service"). I stored my settings within the Web.Config (and the Web.Debug.Config and Web.Release.Config flavors):

<configuration>

    <appSettings>
        <add key="MyAppSettingKey" value="MyAppSettingValue" />
    </appSettings>

    <system.web>
        <compilation debug="true" targetFramework="4.5.1" />
    </system.web>

</configuration>

And in code, I used the ConfigurationManager library:

string mySetting = ConfigurationManager.AppSettings["MyAppSettingKey"];

Further Discussion

Feel free to examine this MSDN thread for further discussion on this very topic.

OTHER TIPS

I had a similar problem after I'd upgraded from Azure SDK 2.2 to 2.3. It seems that after I upgraded my project, the Solution -> Startup project was changed from the Azure Project to the Web Role Project. The result was when I ran the solution the Web Role Project was run in IIS Express, rather than within the Azure Emulator. Changing the Startup Project for the solution back to the Azure Project fixed this particular problem.

As per my understanding,

CloudConfigurationManager.GetSetting will look into web.config if you're running out of a cloud service or locally.

Please add your settings from ServiceConfiguration.Cloud.cscfg file to web.config file, It will work.

For e.g:
In ServiceConfiguration.Cloud.cscfg file:
       <Setting name="SqlServerConnectionString" value="your connections string" />
Add
       <add key="SqlServerConnectionString" value="your connections string" />

Let me know if you have any issue.

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.
  2. Right click to Manage NuGet Packages. On the left click Updates and update WindowsAzure.ConfigurationManager.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top