Azure的WebRole CloudConfigurationManager.GetSetting返回null [英] Azure WebRole CloudConfigurationManager.GetSetting returns null

查看:206
本文介绍了Azure的WebRole CloudConfigurationManager.GetSetting返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebRole项目中的Azure云计算解决方案。

I have an Azure Cloud solution with a WebRole project.

Windows Azure Tools version: 2.2

$ B在WebRole项目
$ b

packages.config包含此:

packages.config in the WebRole project contains this:

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



我有的NuGet启用包还原,因此假设正确的软件包编译过程中链接和参考。

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

下面这行工作在我开发框Azure计算模拟器罚款,但是,当我部署(签入到TFS在线,它建立了解决方案,并揭开序幕部署Azure的),这条线返回null:

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")

我读过一对夫妇在这个其他职位,但至今没有能够解决这个问题。是否有一个包/ SDK不兼容的地方,我不知道的?

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?

推荐答案

原来,这个问题要与我如何配置我的Azure项目,我下面会解释。我也 。问MSDN论坛的问题,并得到了一些帮助,有

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; 在Visual Studio ,我的项目是设立一个Azure的云服务与WebRole。不过,我是部署该项目生产的天青网站。有一个WebRole 的的Azure云服务之间的重要区别和 Azure的网站即可。

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.

下面是一些更多的细节:Azure中可以承载一个网站/ web服务作为两个不同的项目类型和服务配置的一部分。当你走你的Azure门户网站,并创建一个新的服务,您可以创建:

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云服务

  2. Azure的网站

这是Azure的云服务可能会或可能不会包含 WebRole 。如果确实如此,那么它会通过像这样的 http://servicename.cloudapp.net 。蔚蓝的网站上,另一方面,托管在这样的一个端点 http://websitename.azurewebsites.net 。的差异是远远超出了仅仅是域名。

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(您可以创建为云服务的一部分,解决方案在Visual Studio)连接到的云服务您已经创建,因此可以参考和使用属于云服务资源,如您存储中的设置和配置<休息STRONG> .cscfg 文件。这些设置是正确解析,并引用 CloudConfigurationManager 库。

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.

一个网站,但是,是一个独立的项目,就像一个Web应用程序你部署到IIS快递。一个网站基本上是映射到Visual Studio中的Web应用程序的解决方案。在 CloudConfigurationManager 库将不知道如何处理这种类型的项目,即使你在你的web应用程序引用它来做。

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.

无论如何,我重新创建我的项目为Web应用程序解决方案这个时候(而不是云服务)。我保存我的设置Web.Config中内部(和Web.Debug.Config和Web.Release.Config口味):

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>

和在代码中,我使用了 ConfigurationManager中库:

And in code, I used the ConfigurationManager library:

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



进一步讨论



随意检查这个MSDN螺纹关于这个题目的进一步讨论。

Further Discussion

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

这篇关于Azure的WebRole CloudConfigurationManager.GetSetting返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆