Azure WebRole CloudConfigurationManager.GetSetting returns null

僤鯓⒐⒋嵵緔 提交于 2019-12-06 02:35:22

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.

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