为什么Azure CloudConfigurationManager.GetSetting返回null

时间:2015-06-04 17:37:06

标签: asp.net-mvc azure azure-storage asp.net-mvc-5.2 webactivator

我有一个云服务Web角色项目,它在模拟器中本地运行,但在部署时不运行。给出的错误如下:

[ArgumentNullException: Value cannot be null.
Parameter name: connectionString]
   Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(String connectionString) in e:\projects\azure-sdk-for-net\microsoft-azure-api\Services\Storage\Lib\Common\CloudStorageAccount.cs:344
   Candor.WindowsAzure.Storage.Table.CloudTableProxy`1.GetTable() in c:\Users\micha_000\Documents\GitHub\candor-common\Candor.WindowsAzure\Storage\Table\CloudTableProxy.cs:66
   Candor.WindowsAzure.Storage.Table.CloudTableProxy`1.Get(String partitionKey, String rowKey) in c:\Users\micha_000\Documents\GitHub\candor-common\Candor.WindowsAzure\Storage\Table\CloudTableProxy.cs:117
   Candor.WindowsAzure.Logging.Common.Table.CloudTableLogger.get_Configuration() +218
   Candor.WindowsAzure.Logging.Common.Table.CloudTableLogger.get_IsInfoEnabled() +9
   Common.Logging.Factory.AbstractLogger.Info(Object message) in c:\_oss\common-logging\src\Common.Logging.Core\Logging\Factory\AbstractLogger.cs:503
   Candor.Configuration.Provider.ProviderCollection`1.SetActiveProvider(T provider) in c:\Users\micha_000\Documents\GitHub\candor-common\Candor\Configuration\Provider\ProviderCollection.cs:169
   Candor.Configuration.Provider.ProviderResolver`1.AppendActive(T provider) in c:\Users\micha_000\Documents\GitHub\candor-common\Candor\Configuration\Provider\ProviderResolver.cs:77
   SHM.PublicMvcWeb.App_Start.ProviderBootstrapper.InitProviders() in c:\Users\micha_000\Documents\Git-Repos\shm-main\SHM.PublicMvcWeb\App_Start\ProviderBootstrapper.cs:23
   SHM.PublicMvcWeb.App_Start.ProviderBootstrapper.PostStartup() in c:\Users\micha_000\Documents\Git-Repos\shm-main\SHM.PublicMvcWeb\App_Start\ProviderBootstrapper.cs:18

您可以在github上看到堆栈跟踪中显示的相关行的代码。

https://github.com/michael-lang/candor-common/

导致CloudTableProxy错误的行是:

            if (String.IsNullOrWhiteSpace(_connectionName))
                throw new InvalidOperationException("The Cloud ConnectionName has not been configured.");
            if (_account == null)
                _account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting(_connectionName));

因为它没有抛出自定义错误"云连接名称尚未配置。",它告诉我传入CloudConfigurationManager.GetSetting的_connectionName不为null。因此,CloudConfigurationManager是为提供的名称返回null的调用。鉴于此名称在本地运行时确实返回一个值,我不确定为什么在部署到我的云Web角色时找不到它。如果连接名称输入为拼写错误,那么它也不会在本地工作。可以肯定的是,这是我的常用日志记录配置,命名要使用的连接:

  <common>
    <logging>
      <factoryAdapter type="Candor.WindowsAzure.Logging.Common.Table.CloudTableLoggerFactoryAdapter, Candor.WindowsAzure.Logging.Common">
        <arg key="connectionName" value="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" />
      </factoryAdapter>
    </logging>
  </common>

我使用相同的Candor.Common组件的云服务Web角色也有完全不同的解决方案,所以我不相信它们是问题所在。

我使用相同的笔记本电脑来开发这两种解决方案,因此两者都使用Azure 2.2 Tools版本。在每个解决方案中检查Cloud Service项目的属性都会验证这一点。

我检查了每个组件和NuGet包引用,以确保解决方案中没有版本不匹配。在工作解决方案和此非工作解决方案之间,相同的组件设置为copy-local = true。它们也具有相同的绑定重定向。虽然这是我最大的问题,现在已解决,但在遇到此连接问题之前。

非工作服务部署配置:

<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="SHM.AzureService.PublicMvcWeb" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2013-10.2.2">
  <Role name="SHM.PublicMvcWeb">
    <Instances count="1" />
    <ConfigurationSettings>
      <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=(redacted);AccountKey=(redacted)" />
      <Setting name="DefaultTableConnection" value="DefaultEndpointsProtocol=https;AccountName=(redacted);AccountKey=(redacted)" />
      <Setting name="UserTableConnection" value="DefaultEndpointsProtocol=https;AccountName=(redacted);AccountKey=(redacted)" />
      <Setting name="UserSaltTableConnection" value="DefaultEndpointsProtocol=https;AccountName=(redacted);AccountKey=(redacted)" />
      <Setting name="UserAuditTableConnection" value="DefaultEndpointsProtocol=https;AccountName=(redacted);AccountKey=(redacted)" />
    </ConfigurationSettings>
  </Role>
</ServiceConfiguration>

我最近也尝试将这些相同的连接名称放在Web应用程序的appsettings中。这是对3年前评论他们的代码在RoleEnvironment.OnStart之前执行的回应。但是这种尝试仍会导致同样的错误。

这是尝试的样子:

  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=(redacted);AccountKey=(redacted)"/>
    <add key="DefaultTableConnection" value="DefaultEndpointsProtocol=https;AccountName=(redacted);AccountKey=(redacted)"/>
    <add key="UserTableConnection" value="DefaultEndpointsProtocol=https;AccountName=(redacted);AccountKey=(redacted)"/>
    <add key="UserSaltTableConnection" value="DefaultEndpointsProtocol=https;AccountName=(redacted);AccountKey=(redacted)"/>
    <add key="UserAuditTableConnection" value="DefaultEndpointsProtocol=https;AccountName=(redacted);AccountKey=(redacted)"/>
  </appSettings>

错误的代码确实在使用WebActivator 2.0.6启动应用程序时运行,而我的工作解决方案使用WebActivator 2.0.4。但是,WebActivator的发行说明只显示了从Debug到&#34; Retail&#34;两个版本之间的许可证更改,这些更改是在一年前。

如果时间问题,我也尝试在错误调用之前添加一个Thread.Sleep(1000),它也没有工作,所以我将删除它。

using System.Threading;
using Candor.Configuration.Provider;
using Candor.Security;
using Candor.Security.Cryptography;
using Candor.Security.Web;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(StopHarassingMe.PublicMvcWeb.App_Start.ProviderBootstrapper), "PreStartup")]
[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(StopHarassingMe.PublicMvcWeb.App_Start.ProviderBootstrapper), "PostStartup")]

namespace SHM.PublicMvcWeb.App_Start
{
    public class ProviderBootstrapper
    {
        public static void PreStartup()
        {
        }
        public static void PostStartup()
        {
            Thread.Sleep(1000); //let RoleEnvironment finish startup first or connectionstrings are not available.
            InitProviders();
        }

        private static void InitProviders()
        {
            ProviderResolver<HashProvider>.Configure()
                .AppendActive(new SHA2HashProvider("sha2") { IsObsolete = false, SaltModifier = "" });
        }
    }
}

这是在工作和非工作解决方案中配置的第一个提供商。

以下是错误的Web应用程序使用的软件包列表:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.5.0.2" targetFramework="net45" />
  <package id="Candor.Core" version="1.4.1.0" targetFramework="net45" />
  <package id="Candor.jQuery.AutoAsync" version="4.0.0.50129" targetFramework="net45" />
  <package id="Candor.Security" version="2.4.0.0" targetFramework="net451" />
  <package id="Candor.Security.AzureStorageProvider" version="2.3.2.0" targetFramework="net451" />
  <package id="Candor.Web.Mvc" version="1.0.3.0" targetFramework="net451" />
  <package id="Candor.Web.Mvc.ErrorHandler" version="1.0.0.0" targetFramework="net45" />
  <package id="Candor.Web.Mvc.Security" version="2.1.0.0" targetFramework="net45" />
  <package id="Candor.WindowsAzure" version="1.3.0.0" targetFramework="net451" />
  <package id="Candor.WindowsAzure.Logging.Common" version="1.1.1.0" targetFramework="net451" />
  <package id="Common.Logging" version="2.2.0" targetFramework="net45" />
  <package id="Common.Logging.Core" version="2.2.0" targetFramework="net45" />
  <package id="form2js" version="1.0.0.30224" targetFramework="net45" />
  <package id="jQuery" version="2.1.4" targetFramework="net451" />
  <package id="jQuery.UI.Combined" version="1.8.20.1" targetFramework="net45" />
  <package id="jQuery.Validation" version="1.13.1" targetFramework="net45" />
  <package id="MarkdownSharp" version="1.13.0.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net451" />
  <package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net451" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.1.0" targetFramework="net45" />
  <package id="Modernizr" version="2.8.3" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
  <package id="Respond" version="1.4.2" targetFramework="net45" />
  <package id="RestSharp" version="105.0.1" targetFramework="net451" />
  <package id="System.Spatial" version="5.6.0" targetFramework="net451" />
  <package id="T4MVC" version="3.7.4" targetFramework="net45" />
  <package id="T4MVCExtensions" version="3.7.4" targetFramework="net45" />
  <package id="Twilio" version="4.0.3" targetFramework="net451" />
  <package id="Twilio.Mvc" version="3.1.15" targetFramework="net451" />
  <package id="Twilio.TwiML" version="3.3.6" targetFramework="net451" />
  <package id="WebActivatorEx" version="2.0.6" targetFramework="net451" />
  <package id="WebGrease" version="1.6.0" targetFramework="net45" />
  <package id="WindowsAzure.Storage" version="2.1.0.3" targetFramework="net45" />
</packages>

我的工作和非工作解决方案之间存在一些差异,但我并不认为这些差异适用于错误。我从MVC 5.0升级到5.2.3,升级了javascript相关软件包,Candor.WindowsAzure从1.2.10升级到1.3。这似乎是相关的,但唯一的代码更改是替换批量更新方法的实现。

工作溶液项目包:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Antlr" version="3.5.0.2" targetFramework="net45" />
  <package id="Candor.Core" version="1.4.1.0" targetFramework="net451" />
  <package id="Candor.jQuery.AutoAsync" version="3.5.0.40210" targetFramework="net451" />
  <package id="Candor.Security" version="2.4.0.0" targetFramework="net451" />
  <package id="Candor.Security.AzureStorageProvider" version="2.3.2.0" targetFramework="net451" />
  <package id="Candor.Web.Mvc" version="1.0.3.0" targetFramework="net451" />
  <package id="Candor.Web.Mvc.ErrorHandler" version="1.0.0.0" targetFramework="net45" />
  <package id="Candor.Web.Mvc.Security" version="2.1.0.0" targetFramework="net45" />
  <package id="Candor.WindowsAzure" version="1.2.10.0" targetFramework="net451" />
  <package id="Candor.WindowsAzure.Logging.Common" version="1.1.1.0" targetFramework="net451" />
  <package id="Common.Logging" version="2.2.0" targetFramework="net451" />
  <package id="Common.Logging.Core" version="2.2.0" targetFramework="net451" />
  <package id="form2js" version="1.0.0.30224" targetFramework="net45" />
  <package id="jQuery" version="2.0.3" targetFramework="net45" />
  <package id="jQuery.UI.Combined" version="1.10.3" targetFramework="net45" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net45" />
  <package id="MarkdownSharp" version="1.13.0.0" targetFramework="net451" />
  <package id="Microsoft.AspNet.Mvc" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.2" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.OData" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages.Data" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebPages.WebData" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net45" />
  <package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net45" />
  <package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.0.0" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.1.0" targetFramework="net45" />
  <package id="Modernizr" version="2.7.1" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="5.0.8" targetFramework="net45" />
  <package id="SlidesJS" version="3.0.4" targetFramework="net451" />
  <package id="System.Spatial" version="5.6.0" targetFramework="net45" />
  <package id="T4MVC" version="3.7.4" targetFramework="net45" />
  <package id="T4MVCExtensions" version="3.7.4" targetFramework="net45" />
  <package id="WebActivatorEx" version="2.0.4" targetFramework="net45" />
  <package id="WebGrease" version="1.6.0" targetFramework="net451" />
  <package id="WindowsAzure.Storage" version="2.1.0.3" targetFramework="net45" />
</packages>

3 个答案:

答案 0 :(得分:0)

我试图将其部署为一个网站只是为了看看会有什么不同。表连接字符串是在早期尝试通过计时问题的appSettings中进行的,因此尝试网站部署选项(而不是云网络角色)是有意义的。

此尝试的错误是

[FileNotFoundException: Could not load file or assembly 'Microsoft.WindowsAzure.ServiceRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.]

[FileNotFoundException: Could not load file or assembly 'Microsoft.WindowsAzure.ServiceRuntime, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.]
   Candor.WindowsAzure.Logging.Common.Table.CloudTableLogger.WriteInternal(LogLevel level, Object message, Exception exception) +0
   Common.Logging.Factory.AbstractLogger.Info(Object message) in c:\_oss\common-logging\src\Common.Logging.Core\Logging\Factory\AbstractLogger.cs:504
   Candor.Configuration.Provider.ProviderCollection`1.SetActiveProvider(T provider) in c:\Users\micha_000\Documents\GitHub\candor-common\Candor\Configuration\Provider\ProviderCollection.cs:166
   Candor.Configuration.Provider.ProviderResolver`1.AppendActive(T provider) in c:\Users\micha_000\Documents\GitHub\candor-common\Candor\Configuration\Provider\ProviderResolver.cs:75

这很奇怪,因为现在将其他工作解决方案Web角色部署到云中,将该引用设置为CopyLocal = false。但是当我在这个非工作解决方案中将其设置为true时,它开始在网站部署中工作。

接下来,我使用此组件设置为CopyLocal = True重新部署了云Web角色,现在它可以正常运行。我不知道为什么在一个项目部署中需要这个,而不是另一个项目,而两个项目都需要其他azure组件为CopyLocal = true。

启动速度仍然存在一个挥之不去的问题,但如果我不明白的话,我会留下另一个问题。由于某种原因,这个较小的项目在启动后进入忙/循环循环,但页面不时加载。这很奇怪,因为它在启动时初始化的依赖项较少,即实例化的提供程序,每个都写入一个日志条目。

答案 1 :(得分:0)

直接指向的问题是 - 如果已定义,CloudConfigurationManager.GetSetting将从Cloud Project设置中读取给定的配置密钥,否则将回退到实际的项目App或Web配置。因此,如果你确定它存在于其中一个地方,它应该可以正常工作。当您确认它在本地工作时,即使在部署之后它也应该以完全相同的方式工作。

可能发生的其他情况是,您为本地和云部署环境配置了多个配置文件,并且您以前部署到云中的云配置文件中可能缺少配置。

答案 2 :(得分:0)

就我而言,我使用SlowCheetah(NuGet包)来转换App.config文件。在Debug中它没有使用Transformation。使用“root”-config文件中的Debug设置解决了我的问题。

相关问题