gpt4 book ai didi

Azure Function 无法访问应用程序设置

转载 作者:行者123 更新时间:2023-12-03 04:51:08 24 4
gpt4 key购买 nike

我正在尝试将当前作为 Webjob 运行的 .NET 应用程序转换为 Azure Function。

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

因此,我创建了一个 Azure Function App,创建了一个 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 中,则它可以正常工作。它也可以完美地用作网络作业。

有什么想法吗?

更新

以防万一这对将来的其他人有帮助,这是基于下面 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 转换为具有应用程序设置。

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

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

关于Azure Function 无法访问应用程序设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42121410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com