【问题标题】:Azure Function and PowerShell: unable to set connection strings with Set-AzWebAppAzure 函数和 PowerShell:无法使用 Set-AzWebApp 设置连接字符串
【发布时间】:2019-04-03 05:04:38
【问题描述】:

出于部署目的,我创建了一个 PowerShell 脚本来设置应用程序设置。这可以通过

正常工作
$currentAppSettings = $app.SiteConfig.AppSettings

$appSettings = @{}
# Add existing App Settings
ForEach ($currentAppSetting in $currentAppSettings) {
    $appSettings[$currentAppSetting.Name] = $currentAppSetting.Value
}

# Add new App Settings
$appSettings["someKey"] = "someValue"

# Update App Settings
Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -AppSettings $appSettings

当我现在使用实体框架时,我还需要设置连接字符串。首先我认为这应该像应用程序设置一样工作,只需添加 ConnectionStrings 参数:

$currentConnectionStrings = $app.SiteConfig.ConnectionStrings

$connectionStrings = @{}

ForEach ($currentConnectionString in $currentConnectionStrings) {
    $connectionStrings[$currentConnectionString.Name] = $currentConnectionString.ConnectionString
}

$connectionStrings["someKey"] = "someValue"

Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -ConnectionStrings $connectionStrings

但这失败并出现错误

Set-AzWebApp:无法验证参数“ConnectionStrings”上的参数。连接字符串类型值对必须是“System.Collections.Hashtable”类型

尽管$connectionStrings 的类型是System.Collections.Hashtable

还尝试使用自定义对象、数组等失败。

如何正确传递连接字符串? 我如何指定类型(例如 SQLAZURE)?

谢谢

【问题讨论】:

  • 对于您的第二个问题,显然您可以使用 Type 2 将类型设置为 SQL Azure。看看这个答案中的代码,尤其是在他们这样做的地方 connectionString.Add(..) stackoverflow.com/a/41219876/2102114

标签: entity-framework powershell azure-functions connection-string


【解决方案1】:

根据 Set-AzwebApp 的文档,ConnectionStrings 应该是 Hastable

Set-AzWebApp
   [[-AppServicePlan] <String>]
   [[-DefaultDocuments] <String[]>]
   [[-NetFrameworkVersion] <String>]
   [[-PhpVersion] <String>]
   [[-RequestTracingEnabled] <Boolean>]
   [[-HttpLoggingEnabled] <Boolean>]
   [[-DetailedErrorLoggingEnabled] <Boolean>]
   [[-AppSettings] <Hashtable>]
   [[-ConnectionStrings] <Hashtable>]
   [[-HandlerMappings] <System.Collections.Generic.IList`1[Microsoft.Azure.Management.WebSites.Models.HandlerMapping]>]
   [[-ManagedPipelineMode] <String>]
   [[-WebSocketsEnabled] <Boolean>]
   [[-Use32BitWorkerProcess] <Boolean>]
   [[-AutoSwapSlotName] <String>]
   [-ContainerImageName <String>]
   [-ContainerRegistryUrl <String>]
   [-ContainerRegistryUser <String>]
   [-ContainerRegistryPassword <SecureString>]
   [-EnableContainerContinuousDeployment <Boolean>]
   [-HostNames <String[]>]
   [-NumberOfWorkers <Int32>]
   [-AsJob]
   [-AssignIdentity <Boolean>]
   [-HttpsOnly <Boolean>]
   [-AzureStoragePath <WebAppAzureStoragePath[]>]
   [-ResourceGroupName] <String>
   [-Name] <String>
   [-DefaultProfile <IAzureContextContainer>]
   [<CommonParameters>]

您应该使用Hashtable 设置connectionstring,如下所示:

$connectionStrings = @{connectionStrings = @{Name="<ConnectionStringName>";Value="<ConnectionSyring>";type="<SQLAzure/SQLServer/Custom/MySql>"}}

Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -ConnectionStrings $connectionStrings

【讨论】:

    【解决方案2】:

    @KetanChawda-MSFT:感谢您的建议,它对我开发解决方案有很大帮助(但在我的上下文中并没有像下面提到的那样工作)。

    这是我现在的工作方式:

    $currentConnectionStrings = $app.SiteConfig.ConnectionStrings
    $connectionStrings = @{}
    
    # Add existing connection strings
    ForEach ($currentConnectionString in $currentConnectionStrings) {
        $connectionStrings[$currentConnectionString.Name] =  
            @{
                Value=$currentConnectionString.ConnectionString;
                Type=($currentConnectionString.Type | Out-String).ToUpper()
            }
    }
    
    # Add database connection string
    $connectionStrings["connName"] = @{
        Value="connString";
        Type="SQLAZURE"
    }
    
    # Update settings
    Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -ConnectionStrings $connectionStrings
    

    有趣的是,对于现有的连接字符串,我必须将其格式化为字符串并使用 toUpper,否则它对我不起作用(错误:Set-AzWebApp : Cannot validate argument on parameter 'ConnectionStrings'. Connection string type must be specified.)。添加新的连接字符串时,我可以使用例如SQLAZURESQLAzure

    编辑:

    这是一种在使用 Entity Framework 和 Azure Functions 时根本不需要连接字符串部分的方法:Missing ProviderName when debugging AzureFunction as well as deploying azure function

    【讨论】:

    • @quervernetzt,我看不出与我的有任何区别。无论如何,我很高兴能帮助您获得解决方案。
    • @KetanChawda-MSFT:您的回答提供了一般指导,但因此无法解决我的问题。如果您比较您和我的答案中的代码,则会存在一些差异(例如,如何创建哈希表中的条目、处理已经存在的连接字符串、处理与现有连接字符串相关的问题等等)。但您的建议仍然引导我走上正确的道路。
    猜你喜欢
    • 2019-03-29
    • 2019-10-16
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 2022-11-01
    • 2022-06-10
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多