首页 文章

Azure功能无法访问应用程序设置

提问于
浏览
0

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

应用程序访问Azure SQL数据库并从门户中的“应用程序设置”获取连接字符串 . 目前,这是网站App Service中的App Settings(因为它是一个Webjob) .

所以我创建了一个Azure功能应用程序,创建了一个Powershell定时触发器,在Run.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)";

我已经将连接字符串添加到功能应用程序的应用程序设置中,但是当我运行该功能时,我收到错误:找不到命名连接字符串myconnectionstring

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

有任何想法吗?

UPDATE

只是以防万一这对未来的其他人有帮助,这里的Powershell基于David的答案如下:

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

1 回答

  • 4

    实际上,如果你只是启动exe,这将无法工作 . 它在WebJobs中的工作方式是它复制你的exe并转换.exe.config以进行appsetting .

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

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

相关问题