Azure 函数未访问应用程序设置

Azure Function not accessing App Settings

我正在尝试将我目前 运行ning 作为 Webjob 的 .NET 应用程序转换为 Azure 函数。

应用程序访问 Azure SQL 数据库并从门户中的应用设置获取连接字符串。目前这是网站应用服务中的应用设置(因为它是 Webjob)。

所以我创建了一个 Azure Function App,创建了一个 Powershell Timed Trigger 并且在 运行.ps1 我有:

Write-Output "PowerShell Timer trigger function executed at:$(get-date)";
cd D:\home\site\wwwroot\MyApplication\MyApplication.exe
Write-Output "PowerShell completed at:$(get-date)";

并且我已经将连接字符串添加到 Function App 的 App Settings,但是当我 运行 Function 时,我收到错误:找不到命名的连接字符串 myconnectionstring

如果我将连接字符串直接添加到 MyApplication.exe.config 中,那么它将完美运行。它也可以完美地用作 Webjob。

有什么想法吗?

更新

以防万一这对以后的其他人有帮助,下面是基于 David 回答的 Powershell:

$ConnStr = $env:SQLAZURECONNSTR_Nhibernate
$ConfigName = "MyApp.exe.config"
$Xml = [xml](gc $ConfigName)
$Node = $Xml.configuration.connectionStrings.ChildNodes | where {$_.name -eq "nhibernate" }
If($Node.connectionString -ne $ConnStr) {
    Write-Output "Updating Connection String";
    $Node.connectionString = $ConnStr
    $Xml.Save("D:\home\site\wwwroot\MyFunction$ConfigName")
}

确实,如果您只启动 exe,那是行不通的。它在 WebJobs 中的工作方式是复制您的 exe 并将 .exe.config 转换为具有 appsetting.

一个建议的解决方法是让您的应用程序依赖 myconnectionstring 环境变量 而不是应用程序设置。这将起作用,因为所有进程都会自动继承所有这些环境变量。

另一种解决方法是使用应用程序设置的虚拟值部署 MyApplication.exe.config,然后让 PowerShell 脚本在启动进程之前使用 env 变量的值执行字符串替换。并确保它仅在转换尚未完成时才重新保存文件,因为保存操作将使我重新启动该功能。