【问题标题】:Startup fails after deployment when using connection strings configSource attribute along with Azure App Service environment connection strings使用连接字符串 configSource 属性和 Azure 应用服务环境连接字符串时,部署后启动失败
【发布时间】:2017-04-07 15:32:53
【问题描述】:

我正在尝试关注Best practices for private config data and connection strings in configuration in ASP.NET and AzureBest practices for deploying passwords and other sensitive data to ASP.NET and Azure App Service

我采取的步骤:

我有一个带有常规 web.config 文件的 ASP.NET 4.6 Web 应用程序。我为我的秘密创建了两个文件:Web.Secrets.AppSettings.configWeb.Secrets.ConnectionString.config,按照教程将相应的秘密放入其中并修改了根web.config,如下所示:

<configuration>
  <appSettings file="Web.Secrets.AppSettings.config">
  </appSettings>
  <connectionStrings configSource="Web.Secrets.ConnectionStrings.config">
  </connectionStrings>
  <!--...-->
</configuration>

然后我在 Azure 门户中创建了一个新的 Azure 应用程序服务,打开它的 应用程序设置 并将机密添加到相应的部分(应用程序设置连接字符串)。

之后,我将我的 Web 应用程序部署到此 Azure 应用程序服务,然后在启动时出现黄屏死机,并显示以下消息:

“/”应用程序中的服务器错误。

配置错误

说明:处理一个错误 服务此请求所需的配置文件。请查看 下面的具体错误详细信息并修改您的配置文件 适当地。

解析器错误消息:无法打开 configSource 文件 'Web.Secrets.ConnectionStrings.config'。

来源错误

服务器上出现应用程序错误。当前的自定义错误 此应用程序的设置会阻止应用程序的详细信息 远程查看错误(出于安全原因)。它可能, 但是,可以通过在本地服务器计算机上运行的浏览器查看。

源文件:D:\home\site\wwwroot\web.config 行:8

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.6.1590.0

当然Web.Secrets.ConnectionStrings.configWeb.Secrets.AppSettings.config 不会被复制,这正是我所需要的。应从环境变量中获取相应的秘密。

错误页面的 HTML 源代码中有一个堆栈跟踪:

[ConfigurationErrorsException]: Unable to open configSource file 'Web.Secrets.ConnectionStrings.config'. (D:\home\site\wwwroot\web.config line 8)
   at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
   at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
   at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
   at System.Web.Configuration.HttpConfigurationSystem.GetApplicationSection(String sectionName)
   at System.Web.Configuration.HttpConfigurationSystem.GetSection(String sectionName)
   at System.Web.Configuration.HttpConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String configKey)
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.get_ConnectionStrings()
   at EnvSettings.SettingsProcessor.SetConnectionString(String name, String connString, String providerName)
   at EnvSettings.SettingsProcessor.Start()
[InvalidOperationException]: The pre-application start initialization method Start on type EnvSettings.SettingsProcessor threw an exception with the following error message: Unable to open configSource file 'Web.Secrets.ConnectionStrings.config'. (D:\home\site\wwwroot\web.config line 8).
   at System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures)
   at System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods)
   at System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded)
   at System.Web.Compilation.BuildManager.ExecutePreAppStart()
   at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)
[HttpException]: The pre-application start initialization method Start on type EnvSettings.SettingsProc

我做错了什么?还是只是 Azure 中的一个错误?

重要提示

  • 应用程序在启动时失败。 我什至不接触任何与配置文件相关的东西。
  • 如果我从 Azure 应用服务的应用程序设置中的连接字符串部分删除连接字符串,应用程序启动正常。如果我把它拿回来,应用程序在启动时再次开始失败。 这很奇怪!考虑一下!有连接字符串 - 失败,没有连接字符串 - 很好。
  • 当我在本地运行应用程序并且没有Web.Secrets.ConnectionStrings.config 文件时,应用程序运行正常。该应用仅在部署在 Azure 应用服务时才会失败。 因此,问题是特定于 Azure 应用服务的。
  • 即使是一个普通的空 ASP.NET Web 应用程序项目,它也会被复制。
  • appSettings 部分工作正常,只有connectionStrings 部分失败。

默认 Azure 应用服务环境变量:

WEBSITE_NODE_DEFAULT_VERSION:4.4.7

可能的解决方法:

配置转换可用于删除Web.config 中的相应属性。例如,Web.Release.config 可能是这样的:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings xdt:Transform="RemoveAttributes(configSource)"/>
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
</configuration>

【问题讨论】:

  • 是在您尝试查找连接字符串时失败,还是在您执行任何自己的代码之前在启动时失败?
  • 另外,如果你查看错误页面的 HTML 源代码,使用那里的某种堆栈跟踪?
  • @DavidEbbo,它在启动时就失败了。我不会在任何地方碰任何与配置文件相关的东西。我已将堆栈跟踪添加到错误页面 HTML 源中的问题中。
  • @DavidEbbo,当我在本地运行应用程序并且没有Web.Secrets.ConnectionStrings.config 文件时,应用程序运行得很好。该应用仅在部署在 Azure 应用服务时才会失败。我已经更新了关于您的问题的重要说明部分。我认为值得一看。
  • 见下面我的回答。该问题并非特定于 Azure。请尝试我添加到我的答案中的trivial console repro 来证明这一点。

标签: asp.net .net azure web-config azure-web-app-service


【解决方案1】:

我对此进行了研究,我的结论是这不是 Azure 问题,而是配置系统使用连接字符串 configSource 的行为方式。具体来说,行为是当您指定此类指令时,该文件必须存在,否则任何访问连接字符串的尝试都会失败。例如在 Azure 之外,将 web.config 设置为指向缺少的 configSource 并运行:

ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["foo"];

它会以同样的方式爆炸 (Unable to open configSource file 'Web.Secrets.ConnectionStrings.config'.)。

这很有趣,因为使用 App Settings,它可以在文件丢失时简单地忽略 file 指令。

但这些都不是 Azure 特有的。这只是 .NET 框架配置系统行为。我创建了一个简单的控制台应用程序,它证明了:https://github.com/davidebbo-test/ConsoleAppWithMissingConfigSourceFile

您需要拉出属性(如您所展示的那样),或者部署一个虚拟文件以保持其正常运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多